Java如何解析语言环境的数字?
package org.nhooo.example.text;
import java.util.Locale;
import java.text.NumberFormat;
import java.text.ParseException;
public class LocaleNumberParse {
public static void main(String[] args) {
try {
//在此示例中,我们尝试解析
//定义的格式。基本上我们想隐蔽字符串
//区域设置为正确的数字值。
Number number =
NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");
//只需使用解析过程中的数字做一些事情
if (number instanceof Long) {
System.out.println("This number is instanceof Long and the " +
"value is: " + number.longValue());
} else if (number instanceof Double) {
System.out.println("This number is instanceof Double and the " +
"value is: " + number.doubleValue());
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}该代码段打印出以下结果:
This number is instanceof Double and the value is: 25000.75