Java 如何为十进制数执行 Integer.parseInt()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1450991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do an Integer.parseInt() for a decimal number?
提问by Kevin Boyd
The Java code is as follows:
Java代码如下:
String s = "0.01";
int i = Integer.parseInt(s);
However this is throwing a NumberFormatException... What could be going wrong?
然而,这会抛出一个 NumberFormatException ......可能出什么问题了?
采纳答案by Joren
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.
0.01 不是整数(整数),因此您当然不能将其解析为 1。使用Double.parseDouble
或Float.parseFloat
代替。
回答by Martijn Courteaux
String s = "0.01";
double d = Double.parseDouble(s);
int i = (int) d;
The reason for the exception is that an integer does not hold rational numbers (= basically fractions). So, trying to parse 0.3
to a int is nonsense.
A double
or a float
datatype can hold rational numbers.
例外的原因是整数不包含有理数(= 基本上是分数)。因此,尝试解析0.3
为 int 是无稽之谈。Adouble
或float
数据类型可以保存有理数。
The way Java casts a double
to an int
is done by removing the part after the decimal separator by rounding towards zero.
Java 将 a 转换double
为 an 的方式int
是通过向零舍入来删除小数点分隔符之后的部分。
int i = (int) 0.9999;
i
will be zero.
i
将为零。
回答by dominic
Use Double.parseDouble(String a)
what you are looking for is not an integer as it is not a whole number.
使用Double.parseDouble(String a)
您要查找的不是整数,因为它不是整数。
回答by adatapost
Use,
用,
String s="0.01";
int i= new Double(s).intValue();
回答by Swapnil
String s="0.01";
int i = Double.valueOf(s).intValue();
回答by Kevin Tan
use this one
使用这个
int number = (int) Double.parseDouble(s);
int number = (int) Double.parseDouble(s);
回答by Raywell
This kind of conversion is actually suprisingly unintuitive in Java
这种转换实际上在 Java 中非常不直观
Take for example a following string : "100.00"
以下面的字符串为例:“100.00”
C : a simple standard library function at least since 1971(Where did the name `atoi` come from?)
C : 一个简单的标准库函数,至少从 1971 年开始(`atoi` 这个名字从何而来?)
int i = atoi(decimalstring);
Java : mandatory passage by Double (or Float) parse, followed by a cast
Java :强制通过 Double(或 Float)解析,然后是强制转换
int i = (int)Double.parseDouble(decimalstring);
Java sure has some oddities up it's sleeve
Java 肯定有一些奇怪的地方
回答by AnnTea
Using BigDecimal to get rounding:
使用 BigDecimal 进行四舍五入:
String s1="0.01";
int i1 = new BigDecimal(s1).setScale(0, RoundingMode.HALF_UP).intValueExact();
String s2="0.5";
int i2 = new BigDecimal(s2).setScale(0, RoundingMode.HALF_UP).intValueExact();
回答by ayushs27
suppose we take a integer in string.
String s="100"; int i=Integer.parseInt(s); or int i=Integer.valueOf(s);
but in your question the number you are trying to do the change is the whole number
String s="10.00";
double d=Double.parseDouble(s);
int i=(int)d;
假设我们在字符串中取一个整数。
字符串 s="100"; int i=Integer.parseInt(s); 或 int i=Integer.valueOf(s);
但在你的问题中,你试图改变的数字是整数
字符串 s="10.00";
double d=Double.parseDouble(s);
int i=(int)d;
This way you get the answer of the value which you are trying to get it.
这样你就可以得到你试图得到它的价值的答案。