Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Display only 2 digs in string in label.

Display only 2 digs in string in label.

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
16 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Soren Lyder Nielsen

    Hey. I am trying to get my program to display only 2 decimals of the result og an calculation. Code:

    // Defination af input
    dansk = System::Convert::ToDouble(textBox2->Text);
    kurs = System::Convert::ToDouble(numericUpDown1->Text);

             // Udregningerne
             tysk = dansk / kurs;
             ti = tysk \* 0.90;
             femten = tysk \* 0.85;
             tyve = tysk \* 0.82;
             bdansk = tysk \* kurs \* 1.25;
             bti = tysk \* 0.90 \* 1.25;
             bfemten = tysk \* 0.85 \* 1.25;
             btyve = tysk \* 0.82 \* 1.25;
             btysk = tysk \* 1.18;
    
             //konvertering til udskrivning af resultat
             label7->Text = System::Convert::ToString(dansk) + " Kr";
             label8->Text = System::Convert::ToString(ti) + " Kr";
             label9->Text = System::Convert::ToString(femten) + " Kr";
             label10->Text = System::Convert::ToString(tyve) + " Kr";
             label14->Text = System::Convert::ToString(bdansk) + " Kr";
             label15->Text = System::Convert::ToString(bti) + " Kr";
             label16->Text = System::Convert::ToString(bfemten) + " Kr";
             label17->Text = System::Convert::ToString(btyve) + " Kr";
             label20->Text = System::Convert::ToString(tysk) + " Eur";
             label21->Text = System::Convert::ToString(btysk) + " Eur";
    

    My ints are defined another place in my prpgram. Right now my result will be something like 54.867563547, and not 54.86, as my wish is. Can you help ?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Use a format string[^] and numeric formats[^] to do what you want.

    Use the best guess

    S 1 Reply Last reply
    0
    • L Lost User

      Use a format string[^] and numeric formats[^] to do what you want.

      Use the best guess

      S Offline
      S Offline
      Soren Lyder Nielsen
      wrote on last edited by
      #3

      I need a bit more then that... its my second C++ program.. :)

      L 1 Reply Last reply
      0
      • S Soren Lyder Nielsen

        I need a bit more then that... its my second C++ program.. :)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        There are lots of samples in the two links I gave you, that show how you can get your output formatted correctly. This may only be your second program but you need to get familiar with the documentation as soon as possible. For the record you would show the Danish kroner sample as:

                 label7->Text = String.Format("{0:N} Kr", dansk);
        

        However, if you look at the second link it explains how to get a number shown in different currencies automatically, by selecting the appropriate locale.

        Use the best guess

        S 1 Reply Last reply
        0
        • L Lost User

          There are lots of samples in the two links I gave you, that show how you can get your output formatted correctly. This may only be your second program but you need to get familiar with the documentation as soon as possible. For the record you would show the Danish kroner sample as:

                   label7->Text = String.Format("{0:N} Kr", dansk);
          

          However, if you look at the second link it explains how to get a number shown in different currencies automatically, by selecting the appropriate locale.

          Use the best guess

          S Offline
          S Offline
          Soren Lyder Nielsen
          wrote on last edited by
          #5

          I am sorry... But I aint getting anything out of what is explained in the links. I tried to use the: label7->Text = String.Format("{0:N} Kr", dansk); as you said. I just get a lot of errors: warning C4832: token '.' is illegal after UDT 'System::String' error C2275:'System::String' : illegal use of this type as an expression error C2228: left of '.Format' must have class/struct/union I guess its when I remove the Convert part of the line the problem comes ?

          L 1 Reply Last reply
          0
          • S Soren Lyder Nielsen

            I am sorry... But I aint getting anything out of what is explained in the links. I tried to use the: label7->Text = String.Format("{0:N} Kr", dansk); as you said. I just get a lot of errors: warning C4832: token '.' is illegal after UDT 'System::String' error C2275:'System::String' : illegal use of this type as an expression error C2228: left of '.Format' must have class/struct/union I guess its when I remove the Convert part of the line the problem comes ?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            That does not look like C++/CLI code to me; more like C#. I guess you didn't look too closely at the example code[^].

            Use the best guess

            S 1 Reply Last reply
            0
            • L Lost User

              That does not look like C++/CLI code to me; more like C#. I guess you didn't look too closely at the example code[^].

              Use the best guess

              S Offline
              S Offline
              Soren Lyder Nielsen
              wrote on last edited by
              #7

              I am using Microsoft Visual Studio 2010

              L 1 Reply Last reply
              0
              • S Soren Lyder Nielsen

                I am using Microsoft Visual Studio 2010

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                And what is the relevance of that? You do realise that Visual Studio is a development environment and not a programming language?

                Use the best guess

                S 1 Reply Last reply
                0
                • L Lost User

                  And what is the relevance of that? You do realise that Visual Studio is a development environment and not a programming language?

                  Use the best guess

                  S Offline
                  S Offline
                  Soren Lyder Nielsen
                  wrote on last edited by
                  #9

                  Be cource the errorcodes I got was from MS Visual C++ 2010. I thought it might explain to you why I got thoose errorcodes in C++. Its C++ I am Using.

                  L 1 Reply Last reply
                  0
                  • S Soren Lyder Nielsen

                    Be cource the errorcodes I got was from MS Visual C++ 2010. I thought it might explain to you why I got thoose errorcodes in C++. Its C++ I am Using.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    This is the C++/CLI forum; which are you using, C++/CLI or unmanaged C++?

                    Use the best guess

                    S 1 Reply Last reply
                    0
                    • L Lost User

                      This is the C++/CLI forum; which are you using, C++/CLI or unmanaged C++?

                      Use the best guess

                      S Offline
                      S Offline
                      Soren Lyder Nielsen
                      wrote on last edited by
                      #11

                      I have no idea. Thought I could get some help here ?

                      L 1 Reply Last reply
                      0
                      • S Soren Lyder Nielsen

                        I have no idea. Thought I could get some help here ?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Søren Lyder Nielsen wrote:

                        I have no idea.

                        Seriously? You are writing a program and you do not know what language or framework you are using? I don't know how you expect us to help if you cannot answer such a basic question. I would suggest you make a decision which language you plan to use and get hold of a book to learn the basics of it before trying anything else.

                        Use the best guess

                        S 1 Reply Last reply
                        0
                        • L Lost User

                          Søren Lyder Nielsen wrote:

                          I have no idea.

                          Seriously? You are writing a program and you do not know what language or framework you are using? I don't know how you expect us to help if you cannot answer such a basic question. I would suggest you make a decision which language you plan to use and get hold of a book to learn the basics of it before trying anything else.

                          Use the best guess

                          S Offline
                          S Offline
                          Soren Lyder Nielsen
                          wrote on last edited by
                          #13

                          Thanks for the help. But i gotta say, I feel it a is waste of time to try and get anything usefull out of you. All I ask for, is that you write one line of code with my own data in C++. But instead i feel you blame me for not knowing anything, even though that was one of the first things i told you. I will try my luck some other time.

                          L 1 Reply Last reply
                          0
                          • S Soren Lyder Nielsen

                            Thanks for the help. But i gotta say, I feel it a is waste of time to try and get anything usefull out of you. All I ask for, is that you write one line of code with my own data in C++. But instead i feel you blame me for not knowing anything, even though that was one of the first things i told you. I will try my luck some other time.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #14

                            Søren Lyder Nielsen wrote:

                            All I ask for, is that you write one line of code with my own data in C++.

                            No, you have not asked that, you asked how to get a number formatted in a particular way in C++/CLI, and I referred you to the documentation that explains it in detail, with plenty of samples. It seems you are not prepared to spend the time reading it. You also do not seem to know exactly which language you are using, so I cannot answer the question, because it would be different for each language.

                            Søren Lyder Nielsen wrote:

                            But instead i feel you blame me for not knowing anything

                            Neither did I blame you for anything. I have done my best to help you but I cannot answer the question unless I get the proper facts to start with. As I said before, you need to decide which version of the language you are using and start studying it. Give us the proper facts and background to your problem and we will try to help, but you are expected to do some of the work for yourself.

                            Use the best guess

                            1 Reply Last reply
                            0
                            • S Soren Lyder Nielsen

                              Hey. I am trying to get my program to display only 2 decimals of the result og an calculation. Code:

                              // Defination af input
                              dansk = System::Convert::ToDouble(textBox2->Text);
                              kurs = System::Convert::ToDouble(numericUpDown1->Text);

                                       // Udregningerne
                                       tysk = dansk / kurs;
                                       ti = tysk \* 0.90;
                                       femten = tysk \* 0.85;
                                       tyve = tysk \* 0.82;
                                       bdansk = tysk \* kurs \* 1.25;
                                       bti = tysk \* 0.90 \* 1.25;
                                       bfemten = tysk \* 0.85 \* 1.25;
                                       btyve = tysk \* 0.82 \* 1.25;
                                       btysk = tysk \* 1.18;
                              
                                       //konvertering til udskrivning af resultat
                                       label7->Text = System::Convert::ToString(dansk) + " Kr";
                                       label8->Text = System::Convert::ToString(ti) + " Kr";
                                       label9->Text = System::Convert::ToString(femten) + " Kr";
                                       label10->Text = System::Convert::ToString(tyve) + " Kr";
                                       label14->Text = System::Convert::ToString(bdansk) + " Kr";
                                       label15->Text = System::Convert::ToString(bti) + " Kr";
                                       label16->Text = System::Convert::ToString(bfemten) + " Kr";
                                       label17->Text = System::Convert::ToString(btyve) + " Kr";
                                       label20->Text = System::Convert::ToString(tysk) + " Eur";
                                       label21->Text = System::Convert::ToString(btysk) + " Eur";
                              

                              My ints are defined another place in my prpgram. Right now my result will be something like 54.867563547, and not 54.86, as my wish is. Can you help ?

                              U Offline
                              U Offline
                              User 9966263
                              wrote on last edited by
                              #15

                              1 - convert them to float type , example : { double a = 2.1589687489; String^ b = Convert::ToString((float)a); } 2 - remove them , example : { double a = 2.1589687489; String^ b = Convert::ToString(a); b = b->remove(b->IndexOf('.')+2 ); }

                              L 1 Reply Last reply
                              0
                              • U User 9966263

                                1 - convert them to float type , example : { double a = 2.1589687489; String^ b = Convert::ToString((float)a); } 2 - remove them , example : { double a = 2.1589687489; String^ b = Convert::ToString(a); b = b->remove(b->IndexOf('.')+2 ); }

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #16

                                There is no need to do that when you can use format strings to produce the required result in the first place.

                                Use the best guess

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups