久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

java - 如何準確的將科學計數法表示的Double類型轉換為正常的字符串?

瀏覽:120日期:2024-01-15 18:37:59

問題描述

1.起因

最近在做Excel解析,要知道,在Excel中會將長數字自動轉換為科學計數法表示,我的問題是如何原樣 讀取出來并用字符串表示,為了方便說明,我新建一個Workbook,在第一個Sheet的第一個Cell中填入: 123456729.326666,Excel中顯示為:

java - 如何準確的將科學計數法表示的Double類型轉換為正常的字符串?

現在我需要利用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();} }輸出結果

java - 如何準確的將科學計數法表示的Double類型轉換為正常的字符串?

疑問

可以看到,我并沒有得到想要的結果(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

標簽: java
相關文章:
主站蜘蛛池模板: 夹江县| 定兴县| 浦城县| 开江县| 吉水县| 兴安盟| 宜州市| 隆昌县| 无锡市| 桑植县| 万州区| 星座| 达日县| 五莲县| 冷水江市| 汶川县| 穆棱市| 安国市| 棋牌| 沙坪坝区| 卢氏县| 昆明市| 新宁县| 沙雅县| 嵩明县| 贵阳市| 汕头市| 玉溪市| 澄城县| 上林县| 沾化县| 徐闻县| 南靖县| 天柱县| 大英县| 邵东县| 封丘县| 阿拉善盟| 柳河县| 荆门市| 汝南县|