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. javascript - webpack1和webpack2有什么區別?2. Python2中code.co_kwonlyargcount的等效寫法3. django - Python error: [Errno 99] Cannot assign requested address4. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?5. python小白 關于類里面的方法獲取變量失敗的問題6. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。7. java - 線上應用,如果數據庫操作失敗的話應該如何處理?8. python小白,關于函數問題9. [python2]local variable referenced before assignment問題10. python - vscode 如何在控制臺輸入
