Posted on 2009-04-27 00:02
leekiang 阅读(404)
评论(0) 编辑 收藏 所属分类:
java
1,从excel中取得的数字要么带".0",要么是1.33E8之类,用NumberFormat搞定
String value = "";
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
double d = cell.getNumericCellValue();
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setGroupingUsed(false);
value = formatter.format(d);
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
value = cell.getStringCellValue();
}
2, String s="3,4,5,6,";
System.out.println(s.split(",").length);
输出4,我记得以前的输出好像是5?
3, 以正常的方式显示double数字的值,好像比较困难。
public String double2String(double d, int fNumber) {
if (fNumber < 0)
fNumber = 0;
String pattern = null;
switch (fNumber) {
case 0:
pattern = "#0"; //$NON-NLS-1$
break;
default:
pattern = "#0."; //$NON-NLS-1$
StringBuffer b = new StringBuffer(pattern);
for (int i = 0; i < fNumber; i++) {
b.append('#');
}
pattern = b.toString();
break;
}
DecimalFormat formatter = new DecimalFormat();
formatter.applyPattern(pattern);
String value = formatter.format(d);
return value;
}
或
public String SicenToComm(double value) {
String retValue = null;
NumberFormat fmt= NumberFormat.getNumberInstance();
//DecimalFormat fmt= new DecimalFormat(); //效果一样
fmt.setMinimumFractionDigits(5); //
fmt.setMaximumFractionDigits(5); //什么意思
retValue = fmt.format(value);
System.out.println(retValue);
retValue = retValue.replaceAll(",",""); //去掉逗号
return retValue;
}
以上两种方式都不能全部显示543854839493943.4533656这样的数