How to add decimal separator to a grouped (money like) number?

旧城冷巷雨未停 提交于 2019-12-13 03:10:26

问题


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.



来源:https://stackoverflow.com/questions/46380239/how-to-add-decimal-separator-to-a-grouped-money-like-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!