Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format decimal with both thousand separators and forced decimal places

I'd like to String.Format a decimal so that it has both thousand separators and forced decimal places (3).

For example:

Input:

123456,12
78545,8

Output:

123.456,120
78.545,800

I have tried

String.Format("{0:0.0,000}", input);

but this only gives the thousand separators but doesn't force the decimal places.

like image 857
jacobz Avatar asked Jun 29 '14 10:06

jacobz


2 Answers

In a custom numeric format string a period (.) is used for a "localized decimal separator". Ie. even if your current locale uses a comma as a decimal separator you should use a period in a format string. Similarly comma (,) is used for the localised thousands separator.

As your format puts the comma after the period things are going to get confused (thousands separators don't apply after the decimal point).

So try:

 String.Format("{0:#,##0.000}", input);

(Using # for digits to only include if input is large enough.)

like image 87
Richard Avatar answered Oct 13 '22 06:10

Richard


String.Format("{0:N3}", input)

See the N format specifier

like image 45
Lucas Trzesniewski Avatar answered Oct 13 '22 07:10

Lucas Trzesniewski