21

I want to convert a numeric value to a string, displaying culture-specific digits. For example, the Dari language used in Afghanistan (culture name "prs-AF") uses Eastern-Arabic numerals instead of the Arabic numerals used in most Western cultures (0,1,2,3,4,5,6,7,8,9).

When examining the CultureInfo class built into the Framework, it lists the correct native digits (screenshot taken from output in LinqPad):

linqpad output

CultureInfo.CreateSpecificCulture("prs-AF").NumberFormat.NativeDigits

However, when trying to convert a number to a string to display in that culture, I am not getting the native digits:

linqpad output

var number = 123.5;
var culture = CultureInfo.CreateSpecificCulture("prs-AF");
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
var text = number.ToString(culture);
Console.WriteLine(text);

Can anyone tell me how to display the native digits?

Kevin Babcock
  • 10,187
  • 19
  • 69
  • 89
  • 1
    Have you seen this post: http://stackoverflow.com/questions/3055195/string-format-not-converting-integers-correctly-in-arabic/4781769#4781769 ? – Paul Sasik Jun 04 '11 at 22:25
  • `Thread.CurrentThread.CurrentCulture = New CultureInfo("prs-AF");` ?? – Akram Shahda Jun 04 '11 at 22:26
  • 1
    @Paul - I tried to use the answer suggested in that post but it didn't work. If you look at my screenshot above you can see that the default `DigitSubstitution` for the culture "prs-AF" is `NativeNational`. – Kevin Babcock Jun 04 '11 at 22:45
  • As this is a popular question about `DigitSubstitution` property of `Cultures`, I'd like to refer to the useful article "[Bidirectional Features in WPF Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/bidirectional-features-in-wpf-overview?view=netframeworkdesktop-4.8#number-substitution)" on this topic. – Masa Mar 15 '23 at 07:39

3 Answers3

14

Digit substitution is something that takes place when you display text that contain digits.

It is not supposed to change the string representation of a number, as you've seen.

The number 123.5 is formatted as the string 123.5 no matter digit substitution. It is, however, displayed with the appropriate glyphs if Thread.CurrentThread.CurrentCulture is set accordingly and if the presentation engine supports digit substitution. (WPF do support it)

Kevin Babcock
  • 10,187
  • 19
  • 69
  • 89
Mårten Wikström
  • 11,074
  • 5
  • 47
  • 87
  • 1
    +1 and green-checked. Setting Thread.CurrentThread.CurrentCulture causes the digit substitution to work. And while WPF supports this, GDI+ does not, which is why it was not displayed correctly in LINQPad (see the screenshots in the question). – Kevin Babcock Jun 05 '11 at 04:09
  • +1 Great answer. Didn't know there's localization even on strings right before they are displayed. – oleschri Jul 14 '11 at 20:15
3

I looked at NativeDigits propety and the underlying field in Reflector and it doesn't seem to be used by anything when it comes to formatting (although Used by analysis in Reflector is not guaranteed to be 100% full). So it is possible that these values are there just for reference or something like that.

You can use your own IFormatProvider implementation by using the string output of ToString(culture) and manually replacing all digits by corresponding values from NativeDigits array. Although I'm afraid it's not the answer you were looking for..

Dyppl
  • 12,161
  • 9
  • 47
  • 68
  • 3
    I also used Reflector and discovered that formatting takes place in FormatDouble method of internal System.Number class. This method is marked with 'extern' that's why Reflector was unable to analyse its implementation. – StanislawSwierc Jun 04 '11 at 23:29
1

Maybe what you need is this:

var number = 123.5;
var culture = CultureInfo.CreateSpecificCulture("prs-AF");
var text = String.Format(culture, "{0}", number);
Console.WriteLine(text);
oleschri
  • 2,012
  • 11
  • 21
  • 1
    Six years later, this just writes "123,5", which is just the same problem the OP was trying to fix. The question is how to write this in the native digits. – John Hatton Jul 19 '17 at 19:52