Specifying number of digits after decimal in a fixed-point formatted string
-
Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B
-
Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B
double newW = 12.345678; string pw = string.Format("{0:N3}", newW); //pw is now 12.345
-
double newW = 12.345678; string pw = string.Format("{0:N3}", newW); //pw is now 12.345
As always it is very humbling to have the forest pointed out! Thanks for the quick reply. B
-
Hi, Does anyone know of a way to specify the number of decimal places in a fixed-point formatted string in C#? This is how I was doing it in VC++(6.0): CString thisString; double val = 12.3456789; thisString.Format(_T("Format to 4 decimal places %.4f"), val); Much thanks for any help! B
Try this
double MyFloat = 123.456; string MyString; MyString = String.Format("{0:00.00}", MyFloat); Console.WriteLine(MyString);
or even better ;)double MyFloat = 123.456; Console.WriteLine(String.Format("{0:00.00}", MyFloat));
Salil Khedkar [^]