javadouble转科学计数法_如何准确的将科学计数法表⽰的Double类型转换为正常的。。。
1.起因
最近在做Excel解析,要知道,在Excel中会将长数字⾃动转换为科学计数法表⽰,我的问题是如何原样
读取出来并⽤字符串表⽰,为了⽅便说明,我新建⼀个Workbook,在第⼀个Sheet的第⼀个Cell中填⼊:
123456729.326666,Excel中显⽰为:
现在我需要利⽤POI将其读取出来,并原样打印:123456729.326666。
2.我的代码如下:
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 = SheetAt(0).getRow(0).getCell(0);
//获取double类型的值
Double d = NumericCellValue();
/**
* 使⽤NumberFormat转换
*/
NumberFormat nf = Instance();
String s = nf.format(d);
/**
* 使⽤DecimalFormat转换字符串
*/
DecimalFormat df = new DecimalFormat();
s=df.format(d);
/**
* 直接使⽤BigDecimal表⽰
*/
BigDecimal bd = new BigDecimal(d);
//转换为⼯程??我也不知道怎么称呼
s = bd.toEngineeringString();
//使⽤⽹上普遍的解决办法toPlainString
s = bd.toPlainString();
} catch (Exception e) {
e.printStackTrace();
bigdecimal格式化两位小数
}
}
输出结果
疑问
可以看到,我并没有得到想要的结果(123456729.326666),使⽤*Format之后只保留3位⼩数,此处不设置#.####等格式是因为并不确定输⼊的⼩数可以达到多少位,继⽽使⽤了⽹上推荐的toPlainString⽅法得到的结果也并不精确,求各位⼤神指点。