java - 如何準確的將科學計數(shù)法表示的Double類型轉換為正常的字符串?
問題描述
1.起因最近在做Excel解析,要知道,在Excel中會將長數(shù)字自動轉換為科學計數(shù)法表示,我的問題是如何原樣 讀取出來并用字符串表示,為了方便說明,我新建一個Workbook,在第一個Sheet的第一個Cell中填入: 123456729.326666,Excel中顯示為:
現(xiàn)在我需要利用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('科學計數(shù)法表示:nt'+d);//此時打印出來是科學計數(shù)法 /** * 使用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位小數(shù),此處不設置#.####等格式是因為并不確定輸入的小數(shù)可以達到多少位,繼而使用了網上推薦的toPlainString方法得到的結果也并不精確,求各位大神指點。
問題解答
回答1:自問自答:使用cell.getNumericCellValue()得到double數(shù)據(jù)再轉換成字符串doubleStr,當doubleStr.contains('E')為真時調用一下方法:
private static String getRealNumOfScientificNotation(String doubleStr) {int indexOfE = doubleStr.indexOf(’E’);int indexOfPoint = doubleStr.indexOf(’.’);// 整數(shù)部分BigInteger zs = new BigInteger(doubleStr.substring(0, indexOfPoint));// 小數(shù)部分BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint+ BigInteger.ONE.intValue(), indexOfE));// 指數(shù)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再轉換一下,指定一下小數(shù)點位數(shù)再輸出:
/** * 轉換數(shù)值為指定位數(shù) * @param value 原始數(shù)值的字符串表示形式 * @param scale 保留小數(shù)點位數(shù) * @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. mysql - AttributeError: ’module’ object has no attribute ’MatchType’2. php自學從哪里開始?3. javascript - 百度echarts series數(shù)據(jù)更新問題4. MySQL客戶端吃掉了SQL注解?5. 求大神幫我看看是哪里寫錯了 感謝細心解答6. javascript - JS設置Video視頻對象的currentTime時出現(xiàn)了問題,IE,Edge,火狐,都可以設置,反而chrom卻...7. javascript - 圖片能在網站顯示,但控制臺仍舊報錯403 (Forbidden)8. python小白的基礎問題 關于while循環(huán)的嵌套9. phpstady在win10上運行10. python - Django分頁和查詢參數(shù)的問題
