最近在做Excel解析,要知道,在Excel中會(huì)將長(zhǎng)數(shù)字自動(dòng)轉(zhuǎn)換為科學(xué)計(jì)數(shù)法表示,我的問(wèn)題是如何原樣
讀取出來(lái)并用字符串表示,為了方便說(shuō)明,我新建一個(gè)Workbook,在第一個(gè)Sheet的第一個(gè)Cell中填入:
123456729.326666,Excel中顯示為:
現(xiàn)在我需要利用POI將其讀取出來(lái),并原樣打印:123456729.326666。
public static void main(String[] args) throws FileNotFoundException, IOException {
String filePath="C:/Users/yan/Desktop/testDouble.xlsx";
File file = null;
InputStream is = null;
try {
file=new File(filePath);
is = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(is);
XSSFCell cell1 = workbook.getSheetAt(0).getRow(0).getCell(0);
//獲取double類型的值
Double d = cell1.getNumericCellValue();
System.err.println("科學(xué)計(jì)數(shù)法表示:\n\t"+d);//此時(shí)打印出來(lái)是科學(xué)計(jì)數(shù)法
/**
* 使用NumberFormat轉(zhuǎn)換
*/
NumberFormat nf = NumberFormat.getInstance();
String s = nf.format(d);
System.err.println("經(jīng)過(guò)NumberFormat格式化后:\n\t"+s);
/**
* 使用DecimalFormat轉(zhuǎn)換字符串
*/
DecimalFormat df = new DecimalFormat();
s=df.format(d);
System.err.println("經(jīng)過(guò)DecimalFormat格式化后:\n\t"+s);
/**
* 直接使用BigDecimal表示
*/
BigDecimal bd = new BigDecimal(d);
//轉(zhuǎn)換為工程??我也不知道怎么稱呼
s = bd.toEngineeringString();
System.err.println("toEngineeringString表示:\n\t" + s);
//使用網(wǎng)上普遍的解決辦法toPlainString
s = bd.toPlainString();
System.err.println("toPlainString表示:\n\t" + s);
} catch (Exception e) {
e.printStackTrace();
}
}
可以看到,我并沒(méi)有得到想要的結(jié)果(123456729.326666
),使用*Format之后只保留3位小數(shù),此處不設(shè)置#.####
等格式是因?yàn)椴⒉淮_定輸入的小數(shù)可以達(dá)到多少位,繼而使用了網(wǎng)上推薦的toPlainString方法得到的結(jié)果也并不精確,求各位大神指點(diǎn)。
光陰似箭催人老,日月如移越少年。
?? ?? ? ??: cell.getNumericCellValue()
? ???? double ???? ??? ??? doubleStr? ?????. doubleStr.contains("E")? true? ??
Double ???? ??? ??? ????. BigDecimal? ???? ?? ???? ??? ?? ???? ??? ?? ??? ? ????.
????toStandardString("123456729.32666599750518798828125", 6);//??? 123456729.3266666???