开发者

C# WPF Converting english numbers to arabic numbers

开发者 https://www.devze.com 2022-12-10 05:56 出处:网络
I need to display an english double in arabic numerical characters for an application I am working on.

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?


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;


This looks like it does what you need:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消