如何为十进制数做Integer.parseInt()? [英] How to do an Integer.parseInt() for a decimal number?

查看:92
本文介绍了如何为十进制数做Integer.parseInt()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java代码如下:

String s = "0.01";
int i = Integer.parseInt(s);

然而这是抛出一个NumberFormatException ...可能出现什么问题?

However this is throwing a NumberFormatException... What could be going wrong?

推荐答案

0.01不是整数(整数),所以你当然不能把它解析成一个整数。请改用 Double.parseDouble Float.parseFloat

0.01 is not an integer (whole number), so you of course can't parse it as one. Use Double.parseDouble or Float.parseFloat instead.

这篇关于如何为十进制数做Integer.parseInt()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆