how to set digits precision of a float
-
Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.
for rendering, you can use string.format("0.###"), with a # for each level of precision. You can't change what a float can store, however.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
for rendering, you can use string.format("0.###"), with a # for each level of precision. You can't change what a float can store, however.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Thank you very much for your quck help, you gave me a thought of doing it in a different way, the actual code is String.format({0:#.##}, float).
Yes, that also works.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.
Console.WriteLine("{0:N2}",value);
(M.BALA SUBRAMANYAM)
-
Morning! I am calculating a percentage and the float value is, say 12.34567, I know float has 7 digits precision, is there any easy way to set the precision to 4 or 2 digits after .? So that I have 12.3 or 12.34, Thanks a lot.
:) Or simply use Math.Round(floatvalue,2);
Sujith
-
Thank you very much for your quck help, you gave me a thought of doing it in a different way, the actual code is String.format({0:#.##}, float).
hi i have a code like this: float f = d / (c * b * a) / 1000000; string.Format("{0:0.00}",f); txtdansite1.Text = f.ToString(); it still does not work and it has the float`s default precision. help me please
-
Console.WriteLine("{0:N2}",value);
(M.BALA SUBRAMANYAM)
in win application we don`t use console and this syntax does not help,for example: textbox1.text=string.format("{0:n2}", variable); and the textbox1 shows: 0.00
-
:) Or simply use Math.Round(floatvalue,2);
Sujith
float f=2.37037E-08 Math.Round(f, 2); textbox1.Text = f.ToString(); still the same problem and the textbox shows 2.37037E-08 :( :((