How to add decimal separator to a grouped (money l

2019-08-21 22:49发布

问题:

Summary

Are {0:#,##} and {0:#,#} different? I know that the . determines the location of the decimal separator in the result string, and the , serves as both a group separator and a number scaling specifier. As a group separator, it inserts a localized group separator character between each group. As a number scaling specifier, it divides a number by 1000 for each comma specified.

Example

Take a look at some examples:

original    #,##      #,#
12568    -> 12,568    12,568
12568.12 -> 12,568    12,568
1.12     -> 1         1
.12      -> Error    Error

They are all the same even in error.

Question

Why the decimal part does not appear in the examples? How to add decimal separator to a grouped (money like) number?

回答1:

See here: Custom Numeric Format Strings.

The problem is in ,. Your culture decimal separator is ., not ,.

If you replace , by . you will get:

original    #.##       #.#
12568    -> 12568      12568
12568.12 -> 12568.12   12568.1
1.12     -> 1.12       1.1
1.56     -> 1.56       1.6 //new (see rounding)

Also, you can pass needed IFormatProvider that uses , as decimal separator and your example will work with ,.

So, the difference is visible in the last example - there can be rounding.



标签: c# format