views:

902

answers:

3

I need to display an english double in arabic numerical characters for an application I am working on.

Here is an example class that holds the double:

public class Class1
{
    private double someDouble = 0.874;

    public double SomeDouble
    {
        get { return someDouble; }
    }
}

I want to convert the value of SomeDouble to a percentage displayed in Arabic numeric characters at runtime. Here is some quick XAML I've been using as a test:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ArabicNumbers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="ArabicNumbers.Window1"
Title="Window1"
Height="300"
Width="300">
<Window.Resources>
 <local:Class1 x:Key="Class1Instance" />
 <local:DoubleValueConverter x:Key="doubleValueConverter" />
</Window.Resources>

<Grid DataContext="{Binding Source={StaticResource Class1Instance}}">
    <TextBlock
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        TextWrapping="Wrap"
        Margin="10"
        Text="{Binding SomeDouble, Converter={StaticResource doubleValueConverter}, Mode=Default}"/>       
</Grid>

And my test value converter, DoubleValueConverter:

public class DoubleValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double valueAsDouble = (double)value;
        return valueAsDouble.ToString("P1", culture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In the code behind for the window, I set the Language property to the current culture which is "ar-SA". This would seem to be a requirement as this changes the value of the culture parameter in DoubleValueConverter.

public partial class Window1 : Window
{
    public Window1()
    {
        Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name);
        InitializeComponent();
    }
}

This converter provides the correct formatting properties, ie the decimal separator, however I need these numbers to be output as Arabic characters rather than "87,4%".

Does anyone have any suggestions on a simple way to do this?

A: 

We are all currently using Arabic numbers as our numeric system is made by Arabic mathematicians.

D. Veloper
That's what I actually thought but I've been told the expected behaviour is different to what we have at the minute.
Jason
The one thing I don't understand, is why is the expected behaviour different than what we already have when the arabic keyboard in Windows gives numbers like 1 2 3 rather than ١ ٢ ٣
Jason
Actually, the numerical system named "Arabic" was invented by *Indian* mathematicians, and is only called Arabic in the Western world because we got it from Arab traders.
MusiGenesis
A: 

This looks like it does what you need:

http://weblogs.asp.net/abdullaabdelhaq/archive/2009/06/27/displaying-arabic-number.aspx

MusiGenesis
I'd looked at that one but there must be a simpler way. Elsewhere in the project we have numbers being displayed in the equivalent arabic character.The double is actually saved as a string in the class and then bound to in the XAML e.g.MyString = myDouble.ToString("F0", CultureInfo.CurrentCulture);Then bound:<Label Content="{Binding Path=MyString, ElementName=myElementName, Mode=Default}" />I've tried this as well and it didn't work for the section I'm workin on but it doesn't seem to work. I must be missing something more obvious.
Jason
Well, I'm glad I didn't link using www.lmgtfy.com then. This problem is new to me, because I thought that foreign-language sites all use the regular numbers, with the only cultural differences being the `.` and `,` swapping.
MusiGenesis
Yeah me too. Unfortunately I can't show screenshots and actual code of where its working as its "supposed" to so that makes getting further help difficult.The decimal separators are fixed by simply passing the culture into the ToString method but it looks like I will have a lot of searching to do.
Jason
A: 

I've found a very quick and easy solution to this. I discovered that the use of arabic characters for numbers was contextual; ie the digit shape depends on the previous text in the same output.

Example: Entering "1234" as the text in a TextBlock element would simply display this in the english shape. However, if 1234 was preceded with some arabic text (let's say "abcde") such as شلاؤيث1234. In this example, then first four digits would be in the arabic shapes for the characters "1234". (Note: this doesn't seem to work in for this website - displays correctly in the text box however the preview shows english shapes for the numbers)

See these links for more information:

http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution.aspx http://msdn.microsoft.com/en-us/library/system.globalization.digitshapes.aspx

From looking at the second link, the obvious choice I wanted was NativeNational, however since our CurrentCulture is set automatically by the operating system, the CurrentCulture instance is read only.

To by pass this, I simply created a new CultureInfo object and changed the DigitSubstitution to DigitShapes.NativeNational:

        CultureInfo ci = CultureInfo.CreateSpecificCulture(Thread.CurrentThread.CurrentCulture.Name);
        ci.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;
        Thread.CurrentThread.CurrentCulture = ci;
Jason
Worth noting is that the DigitSubstitution can be changed from the control.
Jason