如何以母语格式显示数字

问题描述

我正在尝试以计算机设置的语言显示一列数字。

拿一些示例代码...

    public Form1()
    {
        InitializeComponent();

        Paint += Form1_Paint;
    }

    private void Form1_Paint(object sender,PaintEventArgs e)
    {
        Font font = new Font("Calibri",12.0f);
        decimal amount = 12345.67m;

        e.Graphics.DrawString(amount.ToString("N",CultureInfo.CurrentCulture),font,new SolidBrush(Color.Black),10.0f,10.0f);
    }

我运行它,不出所料,我看到“12,345.67”

enter image description here

如果我将计算机设置为非拉丁语言(例如孟加拉语),我会得到完全相同的输出,而我希望看到它转换为该语言的数字。

将数字推入工具提示、编辑框等中,数字显示为 Windows 已设置为语言的预期,因此我怀疑这与 Graphics 对象有关。

有人能指出我做错的正确方向吗?

编辑:

我希望看到类似以下模型的东西...

enter image description here

编辑 2

将计算机设置为孟加拉语并修改代码(数字只是作为字符串输入)...

enter image description here

当我运行程序时,我仍然得到原始输出(只有英文数字)。

解决方法

我认为这会对您有所帮助:

       private void Form1_Paint(object sender,PaintEventArgs e)
        {
            Font font = new Font("Times New Roman",12.0f);
            var amount = 12345.67m.ToString();
            e.Graphics.DrawString(amount,font,new SolidBrush(Color.Black),10.0f,10.0f);

            e.Graphics.DrawString(ConvertToBengaliNumerals(amount),30.0f);
            e.Graphics.DrawString(ConvertToEasternArabicNumerals(amount),50.0f);

        }
        public static string ConvertToBengaliNumerals(string textAscii)
        {
            return string.Concat(textAscii.ToString().Select(c => (char)('\u09E6' + c - '0')));
        }

        public static string ConvertToEasternArabicNumerals(string input)
        {
            var utf8Encoder = Encoding.UTF8;
            var utf8Decoder = utf8Encoder.GetDecoder();
            var convertedChars = new System.Text.StringBuilder();
            char[] convertedChar = new char[1];
            byte[] bytes = new byte[] { 217,160 };
            char[] inputCharArray = input.ToCharArray();
            foreach (char c in inputCharArray)
            {
                if (char.IsDigit(c))
                {
                    bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
                    utf8Decoder.GetChars(bytes,2,convertedChar,0);
                    convertedChars.Append(convertedChar[0]);
                }
                else
                {
                    convertedChars.Append(c);
                }
            }
            return convertedChars.ToString();
        }

结果:

enter image description here

,

使用 amount.ToString("n",CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name)

       private void Form1_Paint(object sender,PaintEventArgs e)
        {
            Font font = new Font("Calibri",12.0f);
            var amount = 12345.67m;

            //seting culture programmatically:
            CultureInfo.CurrentCulture = new CultureInfo("en-US",false);

            e.Graphics.DrawString(amount.ToString("n",CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name))+" - "+ CultureInfo.CurrentCulture.Name,10.0f);

            CultureInfo.CurrentCulture = new CultureInfo("ru-RU",CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name)) + " - " + CultureInfo.CurrentCulture.Name,30);

            CultureInfo.CurrentCulture = new CultureInfo("eu-ES",50);

            CultureInfo.CurrentCulture = new CultureInfo("de-LI",70);
        }

结果:

enter image description here