Is there any difference between the result of these two format lines?

I inherited some code where prices are formatted as follows:

(1.23d).ToString("£0.00");

I would usually format this currency:

(1.23d).ToString("C");

If the locale of the machine is configured for the UK (as it is), then is there a difference between the two approaches? Can I just make a big find and replace it?

+3
source share
4 answers

You can partially verify that by running this code on a machine with this locale (or forcibly turning it on with Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");):

var nf = CultureInfo.CurrentCulture.NumberFormat;
Console.WriteLine("CurrencyPositivePattern: " + nf.CurrencyPositivePattern);
Console.WriteLine("CurrencyNegativePattern: " + nf.CurrencyNegativePattern);
Console.WriteLine("NegativeSign: " + nf.NegativeSign);
Console.WriteLine("CurrencySymbol: " + nf.CurrencySymbol);
Console.WriteLine("CurrencyDecimalDigits: " + nf.CurrencyDecimalDigits);
Console.WriteLine("CurrencyDecimalSeparator: " + nf.CurrencyDecimalSeparator);
Console.WriteLine("CurrencyGroupSeparator: " + nf.CurrencyGroupSeparator);
Console.WriteLine("CurrencyGroupSizes: " + string.Join(",", nf.CurrencyGroupSizes.Select(gs => gs.ToString())));

Pay attention to the property of group sizes. On my machine, this code:

Console.WriteLine((1232323d).ToString("C"));
Console.WriteLine((1232323d).ToString("£0.00"));

creates two different lines due to groups of numbers.

, , , , - decimal double .

+1

"C" NumberFormatInfo. , , NumberFormatInfo:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
var nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
Console.WriteLine(nfi.CurrencyDecimalDigits);
Console.WriteLine(nfi.CurrencyDecimalSeparator);
Console.WriteLine(nfi.CurrencyNegativePattern);
Console.WriteLine(nfi.CurrencyPositivePattern);
Console.WriteLine(nfi.CurrencySymbol);
+2

That won't make any difference. If the desired currency is selected in the settings of your regional language.

And yes, always prefer decimalas a data type for money.

0
source

(1.23d).ToString("£0.00"); can make it more explicit that numbers always represent (business logic) and that this is not just an arbitrary thing.

0
source

All Articles