首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

String 转 float 有关问题

2012-03-27 
String 转float 问题Stringa1000000000 floatbFloat.parseFloat(a)System.out.println(b)会输出1.0

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;
}

热点排行