android中如何将Double类型转换成Integer类型

记录一下这个知识点,不然老是忘记

(Double.valueOf(positionDataResponse.acb21i).intValue()

来都来了不妨看一下源码吧

    public static Double valueOf(String string) throws NumberFormatException {
        return parseDouble(string);
    }
    public static double parseDouble(String string) throws NumberFormatException {
        return StringToReal.parseDouble(string);
    }
 public static double parseDouble(String s) {
        s = s.trim();
        int length = s.length();

        if (length == 0) {
            throw invalidReal(s, true);
        }

        // See if this could be a named double
        char last = s.charAt(length - 1);
        if (last == 'y' || last == 'N') {
            return parseName(s, true);
        }

        // See if it could be a hexadecimal representation.
        // We don't use startsWith because there might be a leading sign.
        if (s.indexOf("0x") != -1 || s.indexOf("0X") != -1) {
            return HexStringParser.parseDouble(s);
        }

        StringExponentPair info = initialParse(s, length, true);
        if (info.infinity || info.zero) {
            return info.specialValue();
        }
        double result = parseDblImpl(info.s, (int) info.e);
        if (Double.doubleToRawLongBits(result) == 0xffffffffffffffffL) {
            throw invalidReal(s, true);
        }
        return info.negative ? -result : result;
    }
//当你用Integer.valueof("2500.0")时就会出现
    private static NumberFormatException invalidReal(String s, boolean isDouble) {
        throw new NumberFormatException("Invalid " + (isDouble ? "double" : "float") + ": \"" + s + "\"");
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,967评论 25 709
  • 只一次我就爱上了你! 爱上那春风里的温暖, 阳光里的明媚。 只一次我就爱上了你, 爱上那细雨绵绵的温柔, 雷电交加...
    琴罢倚松阅读 1,248评论 0 0
  • 走过青葱的岁月, 漫步静谧的时光。 迈着沉重的步伐, 徘徊于一段段青葱的岁月。 我的心,在自由的梦中流淌。 流进那...
    风雪鹰阅读 2,425评论 0 1
  • I plan to love you for a long time , and never think of g...
    青衫不华阅读 789评论 0 0
  • 从未有过这种迫切的期望,想和某人彻夜畅谈一番。人长大后,不是很多事都可以告诉别人,也并非要全都憋着难受,与人脱节,...
    宛若清晨阅读 1,287评论 0 0