java - 如何準確的將科學計數法表示的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 = workbook.getSheetAt(0).getRow(0).getCell(0); //獲取double類型的值 Double d = cell1.getNumericCellValue(); System.err.println('科學計數法表示:nt'+d);//此時打印出來是科學計數法 /** * 使用NumberFormat轉換 */ NumberFormat nf = NumberFormat.getInstance(); String s = nf.format(d); System.err.println('經過NumberFormat格式化后:nt'+s); /** * 使用DecimalFormat轉換字符串 */ DecimalFormat df = new DecimalFormat(); s=df.format(d); System.err.println('經過DecimalFormat格式化后:nt'+s); /** * 直接使用BigDecimal表示 */ BigDecimal bd = new BigDecimal(d); //轉換為工程??我也不知道怎么稱呼 s = bd.toEngineeringString(); System.err.println('toEngineeringString表示:nt' + s); //使用網上普遍的解決辦法toPlainString s = bd.toPlainString(); System.err.println('toPlainString表示:nt' + s);} catch (Exception e) { e.printStackTrace();} }輸出結果
可以看到,我并沒有得到想要的結果(123456729.326666),使用*Format之后只保留3位小數,此處不設置#.####等格式是因為并不確定輸入的小數可以達到多少位,繼而使用了網上推薦的toPlainString方法得到的結果也并不精確,求各位大神指點。
問題解答
回答1:自問自答:使用cell.getNumericCellValue()得到double數據再轉換成字符串doubleStr,當doubleStr.contains('E')為真時調用一下方法:
private static String getRealNumOfScientificNotation(String doubleStr) {int indexOfE = doubleStr.indexOf(’E’);int indexOfPoint = doubleStr.indexOf(’.’);// 整數部分BigInteger zs = new BigInteger(doubleStr.substring(0, indexOfPoint));// 小數部分BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint+ BigInteger.ONE.intValue(), indexOfE));// 指數int pow = Integer.valueOf(doubleStr.substring(indexOfE+ BigInteger.ONE.intValue()));StringBuffer buffer = new StringBuffer();// e.g. 1.23E-5if (pow < 0) { for (int i = 0; i < -pow; i++) {buffer.append(0); } buffer.insert(BigInteger.ONE.intValue(), '.'); buffer.append(zs.toString()).append(xs.toString()); doubleStr = buffer.toString();} else { int xsLen = xs.toByteArray().length; int needFill0 = pow - xsLen; if (needFill0 < 0) {// e.g. 1.234E2buffer.append(xs);buffer.insert(pow, '.');buffer.insert(0, zs);doubleStr = buffer.toString(); } else {// e.g. 1.234E6 or 1.234E3for (int i = 0; i < needFill0; i++) { buffer.append(0);}buffer.insert(0, xs);buffer.insert(0, zs);doubleStr = buffer.toString(); }}return doubleStr; }回答2:
double 本身就存在精度問題,可以用BigDecimal再轉換一下,指定一下小數點位數再輸出:
/** * 轉換數值為指定位數 * @param value 原始數值的字符串表示形式 * @param scale 保留小數點位數 * @return 轉換后的字符串 */public static String toStandardString(String value, int scale) { return new BigDecimal(value).setScale(scale, BigDecimal.ROUND_HALF_UP).toEngineeringString();}
toStandardString('123456729.32666599750518798828125', 6);// 輸出為123456729.3266666
相關文章:
1. android - NavigationView 的側滑菜單中如何保存新增項(通過程序添加)2. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?3. php - 第三方支付平臺在很短時間內多次異步通知,訂單多次確認收款4. php7.3.4中怎么開啟pdo驅動5. jquery清除input type為password?6. 這段代碼既不提示錯誤也看不到結果,請老師明示錯在哪里,謝謝!7. tp5 不同控制器中的變量調用問題8. 老師 我是一個沒有學過php語言的準畢業生 我希望您能幫我一下9. ueditor上傳服務器提示后端配置項沒有正常加載,求助!!!!!10. 提示語法錯誤語法錯誤: unexpected ’abstract’ (T_ABSTRACT)
