C#WPF转换英文数字来阿拉伯数字 [英] C# WPF Converting english numbers to arabic numbers

查看:411
本文介绍了C#WPF转换英文数字来阿拉伯数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示在我工作的一个应用阿拉伯语数字字符的英文双

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; }
    }
}



我要SomeDouble的值转换为在运行时显示在阿拉伯语数字字符的百分比。这是我一直在使用作为测试一些快速的XAML:

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>



和我的测试值转换器, DoubleValueConverter:

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();
    }
}

在后面的代码为窗口,我设置语言属性设置为当前的文化,这是AR-SA。这似乎是一个要求,因为这改变DoubleValueConverter文化参数的值

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();
    }
}

本转换器提供正确的格式属性,即十进制分离器,但是我需要这些数字来作为阿拉伯字符,而不是87,4%的输出。

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.

例如:
输入1234作为一个TextBlock元素中的文本将简单地显示此在英文的形状。但是,如果1234年之前是与一些阿拉伯文字(比方说ABCDE),如شلاؤيث1234在本实施例,则前四个数字将在阿拉伯形状为字符1234。 (注:这似乎并不在工作本网站 - 在文本框中正确显示但预览显示英文形状为数字)

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

从看第二个链接,我想显而易见的选择是NativeNational,但是因为我们的CurrentCulture是由操作系统自动设置时,的CurrentCulture实例是只读的。

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.

要路过这,我只是创建了一个新的CultureInfo对象,改变了DigitSubstitution到DigitShapes.NativeNational:

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;

这篇关于C#WPF转换英文数字来阿拉伯数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆