【问题标题】:How to show numbers in native language format如何以母语格式显示数字
【发布时间】:2021-07-04 03:43:18
【问题描述】:

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

获取一些示例代码...

    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”

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

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

谁能指出我做错了什么的正确方向?

编辑:

我希望看到类似下面的模型...

编辑 2

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

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

【问题讨论】:

  • 它与Graphics 没有任何关系它必须decimal.ToString 有关,因为一旦你调用它,你所拥有的只是一个@987654331 @,证明dotnetfiddle.net/WEwjz2
  • @Charlieface 但是如果你用非英语语言运行它,那么字符串肯定也是非英语的吗?
  • 您不需要每次绘制表单时都需要一个新的字体对象 - 它会导致您的应用程序严重泄漏。制作一个并重复使用它
  • 不确定你的意思:请告诉我numbers show as expected for the language 应该是什么样子。请记住,这里的大多数人不会说或读孟加拉语
  • @Ňɏssa Pøngjǣrdenlarp 这只是一个快速模拟示例。我的真实代码写得更好。

标签: c# gdi+ tostring


【解决方案1】:

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

       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), font, new SolidBrush(Color.Black), 10.0f, 30.0f);
            e.Graphics.DrawString(ConvertToEasternArabicNumerals(amount), font, new SolidBrush(Color.Black), 10.0f, 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, 0, 2, convertedChar, 0);
                    convertedChars.Append(convertedChar[0]);
                }
                else
                {
                    convertedChars.Append(c);
                }
            }
            return convertedChars.ToString();
        }

结果:

【讨论】:

  • 这是一个非常好的答案。不幸的是,我无法使用它,因为我针对的是多种语言,如果我必须为每种语言编写一个方法,这将变得有些笨拙。在运行我的示例代码时,我不知道计算机设置的语言 - 它可能是中文、法文、德文等。我一直以孟加拉语为例,因为这就是我我目前正在努力。到目前为止,问题仍然存在于我所做的每个非英语字符集上。我只是想让你知道我很感激你的回复。
  • 您可以使用jrgraphix.net/r/Unicode Unicode Character Ranges by Country 来实现它。在 ConvertToBengaliNumerals 方法中,我展示了如何使用
【解决方案2】:

使用 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, font, new SolidBrush(Color.Black), 10.0f, 10.0f);

            CultureInfo.CurrentCulture = new CultureInfo("ru-RU", false);

            e.Graphics.DrawString(amount.ToString("n", CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name)) + " - " + CultureInfo.CurrentCulture.Name, font, new SolidBrush(Color.Black), 10.0f, 30);

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

            e.Graphics.DrawString(amount.ToString("n", CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name)) + " - " + CultureInfo.CurrentCulture.Name, font, new SolidBrush(Color.Black), 10.0f, 50);

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

            e.Graphics.DrawString(amount.ToString("n", CultureInfo.GetCultureInfo(CultureInfo.CurrentCulture.Name)) + " - " + CultureInfo.CurrentCulture.Name, font, new SolidBrush(Color.Black), 10.0f, 70);
        }

结果:

【讨论】:

  • 没有一个回答这个问题:数字1234567 永远不会改变
猜你喜欢
  • 2011-01-16
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2019-02-25
  • 2013-06-23
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多