Let the number x = 0.9503435034. I translate a double in a string of string ^ like this:

str=Convert::ToString(x); т.е str="0.9503435034"; 

And how to do so, to write down a number with two decimal places? For example, the number x = 0.9503435034, and the string to go str = "0.95";

I know there is an overloaded function ToString () of class Convert, it looks like Convert::ToString(double value,IFormatProvider provider) , but I don’t know what to give as the second parameter, please tell me what or other method.

    2 answers 2

    Use custom number formats . To set exactly two characters after a point, the following pattern is appropriate: "0.00":

     String^ str = String::Format("{0:0.00}", x); 

    PS Just as a comment: if you are programming under .NET, work with C # - it will be easier.

    • Thank you very much! It helped! And under .NET in C ++, I am writing because I wrote the coursework in C ++, the neural network model class, and worked with it in the console, and now I needed the graphical interface, so I had to understand visual studio, to be honest quite difficult, but I see no other way out. - Sergey041691
     double x; char str[20]; int l = sprintf (str,"%.2f",x); 

    Note that sprintf () returns the number of characters written, this can be useful when complexly formatting a string to offset the record pointer in a string.

    • // #include <sstream> #include <iomanip> double x; string str; stringstream s; s << setiosflags(ios::fixed) << setprecision(2) << x; str = s.str(); // // #include <sstream> #include <iomanip> double x; string str; stringstream s; s << setiosflags(ios::fixed) << setprecision(2) << x; str = s.str(); // like so - alexlz February