1

I am struggling to apply the correct number format pattern to obtain the following: 1 000 000,00 using something like this"### ###,000". I am aware that by setting the culture in the code, we can specify a region such as fr-CA to obtain the desired formatting without setting a custom pattern. Although this works really well, I am looking for an alternative solution to avoid specifying culture. For some reason, the decimals delimiter is always recognized as being the "." period and I cannot set a comma instead.

glowstarraw
  • 21
  • 1
  • 2

1 Answers1

2

You can use NumberFormatInfo with string.Format.

decimal myDecimal = 12345.67m;

NumberFormatInfo nfi = new NumberFormatInfo { NumberDecimalSeparator = "," };

string formattedNumber = string.Format(nfi, "{0:### ### ###.###}", myDecimal);

Console.WriteLine(formattedNumber); // Prints "12 345,67"

Here's a fiddle for you.

Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44