读书人

String 转 float 有关问题

发布时间: 2012-03-27 13:44:24 作者: rapoo

String 转 float 问题
String a = "1000000000 ";

float b = Float.parseFloat(a);

System.out.println(b);


会输出 1.0E8

我现在想输出正常的数字,怎么解决?

[解决办法]
DecimalFormat df = new DecimalFormat( "0.## ");
df.format(v1);
[解决办法]
you can using String#format when you use JDK1.5
such as;
System.out.println(String.format( "%1f ",b));
[解决办法]
/**
* 格式化位值
* @param sValue 原位值
* @param cType 位型
* @return 整理後的位值
*/
public static String formatField(String sValue, char cType) {
String sDefault;
switch (cType) {
case 'N ': sDefault = "0 "; break;
case 'P ': sDefault = "0.00 "; break;
default: sDefault = " ";
}
if (sValue == null || " ".equalsIgnoreCase(sValue) || "null ".equalsIgnoreCase(sValue)) {
sValue = sDefault;
} else {
sDefault = sValue;
switch (cType) {
case 'D ':
sValue = convertDate(sDefault);
break;
case 'N ':
sValue = (new DecimalFormat( "#,##0 ")).format(Double.parseDouble(sDefault));
break;
case 'P ':
sValue = (new DecimalFormat( "#,##0.00 ")).format(Double.parseDouble(sDefault));
break;
}
}
return sValue;
}

读书人网 >Java Web开发

热点推荐