解析印地语数字时出现 NumberFormatException

2024-05-09

I have an android application and today I have got a crash report which contains this: NumberFormatException

当应用程序尝试解析用户提供的字符串数字时,会触发此异常。

很明显,问题是应用程序无法解析印地语数字!那么,我该如何解决这个问题呢?


Regex

如果你想匹配任何unicode数字,使用正则表达式会更好。正则表达式是\\p{N}+使用方法如下:

Matcher m=Pattern.compile("\\p{N}+").matcher(input);
if(m.find())
{
    System.out.println(m.group());
}

Locale

要回答你的问题,你应该使用数字格式 http://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html如中提到的docs http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf%28java.lang.String%29。指定一个Locale https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html for NumberFormat https://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html.

NumberFormat nf = NumberFormat.getInstance(new Locale("hi", "IN"));
nf.parse(input);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解析印地语数字时出现 NumberFormatException 的相关文章

随机推荐