Formatting and parsing decimals with the Arabic (Saudi Arabia) culture

89 Views Asked by At

Consider the following C# code:

decimal value = -123.456m;
CultureInfo culture = CultureInfo.GetCultureInfo("ar-SA");

string formatted = value.ToString("e", culture);
Console.WriteLine(formatted);

decimal parsed = decimal.Parse(formatted, culture);
Console.WriteLine(parsed);

Allowing for the fact that decimal.Parse doesn't even work in this instance, this code produces some unusual results:

  • -1,234560e +002 is what I see printed in the console.
  • ؜-1٫234560e؜+002 is what I get if I copy the text in the console.
  • 002+1,23456e- is what formatted displays in the debugger.
  • \u061c-1٫234560e\u061c+002 is what I get if I copy the value from the debugger to a new string.

Questions

  1. Why does formatting with ar-SA produce this unusual result?
  2. Why can this not be parsed when providing the exact same culture?

If there is any culture-specific documentation in the .NET docs regarding this sort of formatting, I'd greatly appreciate the links so I can read further into this.

Update

As per this comment from dbc:

Finally, do you get the same issue with double instead of decimal? If double round-trips while decimal does not, that argues for this being a .NET bug with ICU for formatting of decimals using scientific notation. (Incidentally, formatting of decimals using scientific notation looks sort of like an edge case to me. Usually decimals are formatted using standard notation.)

double value = -123.456;
CultureInfo culture = CultureInfo.GetCultureInfo("ar-SA");

string formatted = value.ToString("e", culture);
Console.WriteLine(formatted);

double parsed = double.Parse(formatted, culture);
Console.WriteLine(parsed);

This also produces some unusual results, although in this instance double.Parse works:

  • -1,234560e +002 is what I see printed in the console.
  • ؜-1٫234560e؜+002 is what I get if I copy the text in the console.
  • 002+1,234560e- is what formatted displays in the debugger.
  • \u061c-1٫234560e\u061c+002 is what I get if I copy the value from the debugger to a new string.

Therefore it appears that double.ToString("e", culture) formats the number the same way.

Note: I am using NET 8.0, Rider and MacOS. CurrentCulture is en-GB.

0

There are 0 best solutions below