C++/CLI: Formatting outputs (decimal places, hexadecimal, case notation)
-
Hi, when I was programming in console mode if I needed to display only 2 decimal values of a variable (e.g. Variable1) I would write this simple code:
cout << "Variable1 = " << setprecision (2) << fixed << Variable1 << endl;
But now in C++/CLI environment how can I display this value inside a textbox showing only 2 decimal values? Can I add some attributes to the following code?
textBox1->Text= Variable1.ToString();
And what about if I wanted to display it in Hexadecimal and/or in Upper case notation? Many thanks in advance!
-
Hi, when I was programming in console mode if I needed to display only 2 decimal values of a variable (e.g. Variable1) I would write this simple code:
cout << "Variable1 = " << setprecision (2) << fixed << Variable1 << endl;
But now in C++/CLI environment how can I display this value inside a textbox showing only 2 decimal values? Can I add some attributes to the following code?
textBox1->Text= Variable1.ToString();
And what about if I wanted to display it in Hexadecimal and/or in Upper case notation? Many thanks in advance!
so you decided to use some type's ToString() method but you did not consider reading up about it? :~
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
so you decided to use some type's ToString() method but you did not consider reading up about it? :~
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Object.ToString() has several overloads. some accept formatting instructions and culture specifications. You can get help on MSDN (and in visual studio help). You should consult the ToString(0 documentation for the particular type you are concerned with, as the base class virtual methods are often overridden. The help for Int32[^] is typical, and should shed light on your question. There are many links to additional details on formatting in the help article.
-
Hi, when I was programming in console mode if I needed to display only 2 decimal values of a variable (e.g. Variable1) I would write this simple code:
cout << "Variable1 = " << setprecision (2) << fixed << Variable1 << endl;
But now in C++/CLI environment how can I display this value inside a textbox showing only 2 decimal values? Can I add some attributes to the following code?
textBox1->Text= Variable1.ToString();
And what about if I wanted to display it in Hexadecimal and/or in Upper case notation? Many thanks in advance!
In reply to my question...
textBox1->Text= Variable1.ToString("N0"); //shows value with 0 decimal places
textBox1->Text= Variable1.ToString("N2"); //shows value with 2 decimal places
textBox1->Text= Variable1.ToString("X"); //shows value in Hexadecimal notation (capital case)
textBox1->Text= Variable1.ToString("x"); //shows value in Hexadecimal notation (normal case)Hope it helps somebody out there like me. I am really amazed there isn't a decent manual on these very basic things... It must all be there on MSDN but I find it the most impossible reference to consult.
-
In reply to my question...
textBox1->Text= Variable1.ToString("N0"); //shows value with 0 decimal places
textBox1->Text= Variable1.ToString("N2"); //shows value with 2 decimal places
textBox1->Text= Variable1.ToString("X"); //shows value in Hexadecimal notation (capital case)
textBox1->Text= Variable1.ToString("x"); //shows value in Hexadecimal notation (normal case)Hope it helps somebody out there like me. I am really amazed there isn't a decent manual on these very basic things... It must all be there on MSDN but I find it the most impossible reference to consult.
MSDN string formatting instructions are tricky to find. Pro tip: Don't use ms search engine, it's horrible. use google with site:msdn.....com helper. Google Query used: c# ToString format decimal site:msdn.microsoft.com First link I found: (Almost) http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^] Linked from first: (We have a winner): http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^]
R.Bischoff
Tengas un buen dia