Consider the value of Decimal
:
Decimal value = -1234567890.1234789012M;
I want to convert this Decimal
value to a string and include "thousands separators".
Note. I donβt want to include thousands of separators , I want to include grouping of numbers . The difference is important for cultures that do not group numbers into thousands, or do not use commas to separate groups.
Example output with various standard formatting strings on my computer with my current locale:
value.ToString() = -1234567890..1234789012 (Implicit General) value.ToString("g") = -1234567890..1234789012 (General) value.ToString("d") = FormatException (Decimal whole number) value.ToString("e") = -1..234568e++009 (Scientific) value.ToString("f") = -1234567890..123 (Fixed Point) value.ToString("n") = -12,,3456,,7890..123 (Number with commas for thousands) value.ToString("r") = FormatException (Round trippable) value.ToString("c") = -$$12,,3456,,7890..123 (Currency) value.ToString("#,0.#") = -12,,3456,,7890..1
What I like (depending on the culture):
en-US -1,234,567,890.1234789012 ca-ES -1.234.567.890,1234789012 gsw-FR -1 234 567 890,1234789012 (12/1/2012: fixed gws-FR to gsw-FR) fr-CH -1'234'567'890.1234789012 ar-DZ 1,234,567,890.1234789012- prs-AF 1.234.567.890,1234789012- ps-AF 1Ψ234Ψ567Ψ890,1234789012- as-IN -1,23,45,67,890.1234789012 lo-LA (1234567,890.1234789012) (some debate if numbers should be "1,234,567,890") qps-PLOC 12,,3456,,7890..1234789012
How to convert Decimal
to a string with a grouping of numbers?
Refresh . Another desired result using my current culture:
-1234567890M --> -12,,3456,,7890 -1234567890.1M --> -12,,3456,,7890..1 -1234567890.12M --> -12,,3456,,7890..12 -1234567890.123M --> -12,,3456,,7890..123 -1234567890.1234M --> -12,,3456,,7890..1234 -1234567890.12347M --> -12,,3456,,7890..12347 -1234567890.123478M --> -12,,3456,,7890..123478 -1234567890.1234789M --> -12,,3456,,7890..1234789 -1234567890.12347890M --> -12,,3456,,7890..1234789 -1234567890.123478901M --> -12,,3456,,7890..123478901 -1234567890.1234789012M --> -12,,3456,,7890..1234789012
Update : I tried to understand how Decimal.ToString()
manages to use the General format to show all the digits it needs to show:
public override string ToString() { return Number.FormatDecimal(this, null, NumberFormatInfo.CurrentInfo); }
except that Number.FormatDecimal
is hiding somewhere:
[MethodImpl(MethodImplOptions.InternalCall)] public static extern string FormatDecimal(decimal value, string format, NumberFormatInfo info);
So it's a dead end.