Long.Parse以阿拉伯数字表示(即١是一个整数= 1) [英] Long.Parse for Arabic-Indic digits (i.e. ١ is an integer = 1)

查看:170
本文介绍了Long.Parse以阿拉伯数字表示(即١是一个整数= 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想使用.net Long.Parse("١٢٣٤٥٦٧٨٩٠")来获取1234567890,但它给出了一个错误,并且无法将这些印度数字识别为数字.
我该怎么办?

谢谢,
Mohammad Amiry

Hi,
I would like to use .net Long.Parse("١٢٣٤٥٦٧٨٩٠") to get 1234567890 but it gives an error and does not recognizes these Indic digits as numbers.
How can I do that?

Thanks,
Mohammad Amiry

推荐答案

如果可以将字符串转换为拉丁数字,则可以解析该字符串.我从未使用过国际化和Unicode,但我认为您可以一次遍历字符串中的一个字符,并使用case语句进行比较和翻译.使用StringBuilder(比使用普通字符串更有效)重建字符串,然后将结果传递给Parse.麻烦事了.

一旦有了有效的算法,就可以扩展Int32 [ ^ ]对象添加一个ParseFromIndic函数,这将使您的代码编写变得更加容易.

修订后的答案-做过一些研究,发现扩展方法仅适用于实例变量;从.Net 4.0开始,您不能编写静态扩展名.但是,您可以编写一个名称为StringString扩展名,该扩展名试图将字符串实例的值转换为数字.
If you can translate the string into Latin numerals, you can then parse that string. I''ve never used internationalization and Unicode, but I would think you could step through the string one character at a time and use a case statement to compare and translate. Rebuild the string using a StringBuilder (which is more efficient than concatinating ordinary strings) and pass the result into Parse. A nuisance, certainly.

Once you have an algorithm that works, you could extend the Int32[^] object to add a ParseFromIndic function, which will make your code writing quite a bit easier.

Revised answer - Did some research and found that extension methods only work on instanced variables; as of .Net 4.0, you cannot write static extensions. However, you could write a String extension with a name like ParseIndicNumber which attempts to convert the value of the string instance into a number.


根据MSDN .NET 4文档关于解析数字字符串 [
According to the MSDN .NET 4 doucmentation regarding the Parsing Of Numeric Strings[^] you are, sad but true , on your own.

Regards,

Manfred


这实际上称为印度阿拉伯数字,更确切地称为东方阿拉伯数字",有趣的是阿拉伯人称呼其为印地语数字",而欧洲人(以及许多其他人)称呼0123456789阿拉伯语"数字";参见 http://en.wikipedia.org/wiki/Arabic_digits [
—SA
This is actually called Indo-Arabic numerals, more exactly called "Eastern Arabic", funny that Arabs call then "Hindi numerals", and Europeans (and many other) call 0123456789 "Arabic digits"; see http://en.wikipedia.org/wiki/Arabic_digits[^].

It looks like they are not parsed properly, so I''m afraid this is the only way: they should be replaced with "Arabic digits" (:-)) 01234567890 before parsing albeit not in that naive way mentioned by MohammadAmiry (:-)), but by subtracting of a constant shift from a Unicode code point (please calculate it by yourself, this is too easy; the shift is possible because the order of digits is exactly the same in both systems).

—SA


这篇关于Long.Parse以阿拉伯数字表示(即١是一个整数= 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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