Posted on 2009-04-27 00:02
leekiang 閱讀(408)
評(píng)論(0) 編輯 收藏 所屬分類:
java
1,從excel中取得的數(shù)字要么帶".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數(shù)字的值,好像比較困難。
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(",","");? //去掉逗號(hào)
???????? return retValue;?
???? }
以上兩種方式都不能全部顯示543854839493943.4533656這樣的數(shù)