ate*_*iob 174 java string integer decimal-point
除了Integer.parseInt()
处理减号(如文件所述),Integer.valueOf()
和之间是否还有其他差异Integer.parseInt()
?
因为两者都不能解析,
为十进制千位分隔符(产生NumberFormatException
),有没有一个已经可用的Java方法来做到这一点?
cor*_*iKa 221
实际上,内部valueOf
使用parseInt
.不同之处在于返回一个对象时parseInt
返回一个int
基元.从Integer.class源代码中考虑:valueOf
Integer
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s, 10);
}
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s, radix));
}
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
Run Code Online (Sandbox Code Playgroud)
至于用逗号解析,我对它不熟悉.我会消毒它们.
int million = Integer.parseInt("1,000,000".replace(",", ""));
Run Code Online (Sandbox Code Playgroud)
Mar*_*aux 29
第一个问题:java中parseInt和valueOf的区别?
第二个问题:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();
Run Code Online (Sandbox Code Playgroud)
第三个问题:
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(symbols);
df.parse(p);
Run Code Online (Sandbox Code Playgroud)