如何将英文数字转换为阿拉伯数字?
例如,我有这个 C# 代码
DateTime.Now.ToString("MMMM dd, yyyy");
现在当前线程正在加载阿拉伯文化。所以结果是这样的
???? 19, 2010
但我不希望“2010”和“19”是英语(也称为拉丁语或西阿拉伯数字) - 我想要东阿拉伯数字,例如“٢”。
我尝试
DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.GetCultureInfo("ar-lb"));
给出了相同的结果。 那么有什么想法吗?
I have this C# code for example
DateTime.Now.ToString("MMMM dd, yyyy");
Now the current thread is loading the Arabic culture. So the result is like this
???? 19, 2010
But i don't want the '2010' and the '19' to be in English (also known as Latin or West Arabic digits) - I want East Arabic numbers like "٢".
I tried
DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.GetCultureInfo("ar-lb"));
gave the same result.
So any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用此解决方法(只需列出您想要在字符串数组中使用此数字的所有文化):
然后使用该方法,对于您想要在其中包含“中心阿拉伯数字”的所有
字符串
,如下所示:Thy this workaround (just list all cultures you want to use this numerals in the string array):
Then use the method, for all of your
strings
you want to have 'central Arabic numerals' in, like this:作为一个快速测试,我写了这个来列出当年没有“2010”的所有文化:
结果是:
将数字转换为阿拉伯文本,它看起来像 这个“NumToArabicString”项目可以做到这一点。不过,.net 框架中似乎没有内置任何内容。
As a quick test, I wrote this to list all the cutures which don't have "2010" in the year:
the results are:
To convert the numbers to Arabic text, it looks like this "NumToArabicString" project will do it. It doesn't look like there's anything built into the .net framework though.
唯一的解决办法是手动转换数字。
您可以使用 CurrentCulture.NumberFormat.NativeDigits 数组,而不是显式提供 Unicode 字符来支持除阿拉伯-印度数字之外的任何其他语言,但就文化感知类的内置支持而言,例如作为
DateTime
它似乎未实现或其他什么。The only solution is to manually convert the digits.
You could use the
CurrentCulture.NumberFormat.NativeDigits
array instead of explicitly providing the Unicode characters to support any other languages in addition to the Arabic-Indic digits, but as far as builtin support by culture-aware classes such asDateTime
it seems unimplemented or something.试试这个:
Try this:
我采取了 @Marcel B 方法/解决方法,因为我面临着与原始问题所述相同的问题。
就我而言,它适用于“ar-KW”文化。唯一的区别是我使用的是 NumberFormat.NativeDigits,它已经是 CultureInfo 的一部分。
您可以这样检查(基于您当前的线程场景):
因此,代码将如下所示:
我希望它有所帮助。
I've taken @Marcel B approach/workaround as I'm facing the same issue as the original question states.
In my case, it is for "ar-KW" Culture. The only difference is that I'm using the NumberFormat.NativeDigits which is already part of the CultureInfo.
You can check that like this (based on your current thread scenario):
So, the code will look like this:
I hope it helps.
您可以使用 Windows.Globalization.NumberFormatting .NumeralSystemTranslator 类在拉丁语和任何受支持的数字系统之间进行翻译。要翻译为阿拉伯语,请将
NumeralSystem
属性设置为"Arab"
,然后就可以调用TranslateNumerals
方法。或者,您可以只使用 Windows.Globalization。直接使用 DateTimeFormatting.DateTimeFormatter 类。
You can use the Windows.Globalization.NumberFormatting.NumeralSystemTranslator class to translate between the Latin and any of the supported numeral systems. To translate to Arabic, set the
NumeralSystem
property to"Arab"
, then you can call theTranslateNumerals
method.Alternatively you can just use Windows.Globalization.DateTimeFormatting.DateTimeFormatter class directly.
这也是转换数字 35852 的选项:
输出:
This is also an option to convert number 35852:
Outpus:
我们在英语中使用阿拉伯数字。因此,您所看到的是正确且唯一可能的行为。
We use Arabic numerals in English. As such, what you're seeing is the correct - and only possible - behaviour.