亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - 如何準(zhǔn)確的將科學(xué)計數(shù)法表示的Double類型轉(zhuǎn)換為正常的字符串?
大家講道理
大家講道理 2017-04-18 10:52:02
0
2
1713

1.起因

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

現(xiàn)在我需要利用POI將其讀取出來,并原樣打?。?23456729.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("科學(xué)計數(shù)法表示:\n\t"+d);//此時打印出來是科學(xué)計數(shù)法
            /**
             * 使用NumberFormat轉(zhuǎn)換
             */
            NumberFormat nf = NumberFormat.getInstance();
            String s = nf.format(d);
            System.err.println("經(jīng)過NumberFormat格式化后:\n\t"+s);
            /**
             * 使用DecimalFormat轉(zhuǎn)換字符串
             */
            DecimalFormat df = new DecimalFormat();
            s=df.format(d);
            System.err.println("經(jīng)過DecimalFormat格式化后:\n\t"+s);
            /**
             * 直接使用BigDecimal表示
             */
            BigDecimal bd = new BigDecimal(d);
            //轉(zhuǎn)換為工程??我也不知道怎么稱呼
            s = bd.toEngineeringString();
            System.err.println("toEngineeringString表示:\n\t" + s);
            //使用網(wǎng)上普遍的解決辦法toPlainString
            s = bd.toPlainString();
            System.err.println("toPlainString表示:\n\t" + s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

輸出結(jié)果

疑問

可以看到,我并沒有得到想要的結(jié)果(123456729.326666),使用*Format之后只保留3位小數(shù),此處不設(shè)置#.####等格式是因為并不確定輸入的小數(shù)可以達(dá)到多少位,繼而使用了網(wǎng)上推薦的toPlainString方法得到的結(jié)果也并不精確,求各位大神指點。

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

全部回復(fù)(2)
阿神

自問自答:使用cell.getNumericCellValue()得到double數(shù)據(jù)再轉(zhuǎn)換成字符串doubleStr,當(dāng)doubleStr.contains("E")為真時調(diào)用一下方法:

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-5
        if (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.234E2
                buffer.append(xs);
                buffer.insert(pow, ".");
                buffer.insert(0, zs);
                doubleStr = buffer.toString();
            } else {
                // e.g. 1.234E6 or 1.234E3
                for (int i = 0; i < needFill0; i++) {
                    buffer.append(0);
                }
                buffer.insert(0, xs);
                buffer.insert(0, zs);
                doubleStr = buffer.toString();
            }
        }
        return doubleStr;
    }
迷茫

double 本身就存在精度問題,可以用BigDecimal再轉(zhuǎn)換一下,指定一下小數(shù)點位數(shù)再輸出:

/**
 * 轉(zhuǎn)換數(shù)值為指定位數(shù)
 * @param value 原始數(shù)值的字符串表示形式
 * @param scale 保留小數(shù)點位數(shù)
 * @return 轉(zhuǎn)換后的字符串
 */
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

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板