I have 50172,40
and 500,00
. I would like to print them as 50.172,40
and 500,00
in C# (dot before thousands and comma before cents).
What I have tried:
public static string ToBankString(this decimal value)
{
return value.ToString("N2", CultureInfo.InvariantCulture);
}
But I get 50,172.40
(dot and comma are in wrong order). What can I do?
You can create your own NumberFormatInfo
and use that to format the number:
var value = 50172.40M;
var numberFormat = new NumberFormatInfo();
numberFormat.CurrencyDecimalSeparator = ",";
numberFormat.CurrencyGroupSeparator = ".";
Console.WriteLine(value.ToString("N2", numberFormat));
This will write 50,172.40
.
But perhaps you should use the CultureInfo.CurrentCulture
because that defines how the user prefer to format numbers? This CultureInfo
is the default used if you do not specify any in ToString
.