Hi
how to limit text to 6 numbers . 2 numbers ?
(######.##)
thank's in advance
Hi
how to limit text to 6 numbers . 2 numbers ?
(######.##)
thank's in advance
// without leading zeros
string formatted = String.Format("{0:######.##}", value);
// with leading zeros
string formatted = String.Format("{0:000000.00}", value);
You can use either the String.Format method or the ToString method:
double value = 123.456;
string formatted1 = String.Format(CultureInfo.InvariantCulture, "{0:######.##}", value);
string formatted2 = value.ToString("######.##", CultureInfo.InvariantCulture);
If you use # in the formatting string, those will be filled with spaces if there are no significant digits there. For example 123.456 formatted using "######.##" will be " 123.46".
If you use 0 in the formatting string, those will be filled with zeroes of there are no significant digits there. For example 123.456 formatted using "000000.00" will be "000123.46".
You can combine # and 0 to get different results. For example you might want spaces before the decimal separator, but always at least one digit: "#####0.00".
The period character is used to specify the decimal separator. This is a period for some culture settings and a comma for other. You always use the period in the format string, but the output depends on the culture settings. If you always want a period in the output you can use the CultureInfo.InvariantCulture culture.
If you don't want the number to be rounded, you have to truncate it before formatting it:
double value = 123.456;
value = Math.Truncate(value * 100.0) / 100.0;