在android中加倍的字符串给出了错误的值

时间:2013-12-18 15:23:32

标签: android string double

所以我有这段代码:

String tmp_s = sampleTime.replaceAll("[^\\d]", ""); // after this 20131218125600
tmp_s = tmp_s.substring(4); // after this 1218125600
tmp_s = tmp_s.substring(0, tmp_s.length()-2); // after this 12181256
double d = Double.parseDouble(tmp_s); // after this 1.2161256E12??????????????

我很无能为力,为什么我有1.2161256E12而不是12161256.0的双倍?

字母E来自哪里?

3 个答案:

答案 0 :(得分:1)

double用于表示太小或太大的浮动值。而且你得到的价值太小而且呈指数形式。

答案 1 :(得分:1)

试试这个..

        String sampleTime = "20131218125600";
        String tmp_s = sampleTime.replaceAll("[^\\d]", ""); // after this 20131218125600
        tmp_s = tmp_s.substring(4); // after this 1218125600
        tmp_s = tmp_s.substring(0, tmp_s.length()-2); // after this 12181256
        double d = Double.parseDouble(tmp_s); 
        Log.v("d", ""+d);

        DecimalFormat dF = new DecimalFormat("0.00");
        try {
            Number num = dF.parse(tmp_s);
            Log.v("num", ""+num);
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

1.2161256E712161256.0与此1.2161256*10000000相同,也与1.2161256*10^7

相同

答案 2 :(得分:1)

我的代码有一些变化。 //1.2181256E7
显示完美的价值ans:1.2181256E7检查以下代码..

        String sampleTime="20131218125600";   
        String tmp_s = sampleTime.replaceAll("[^\\d]", "");
        tmp_s = tmp_s.substring(4); // after this 1218125600
        tmp_s = tmp_s.substring(0, tmp_s.length()-2); // after this 12181256
       //2. Use valueOf method of Double class. This method is static.
        Double dObj2 = Double.valueOf(tmp_s);
        System.out.println(dObj2);