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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:25:05  来源:igfitidea点击:

How to do an Integer.parseInt() for a decimal number?

javaparsinginteger

提问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.parseDoubleor Float.parseFloatinstead.

0.01 不是整数(整数),因此您当然不能将其解析为 1。使用Double.parseDoubleFloat.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.3to a int is nonsense. A doubleor a floatdatatype can hold rational numbers.

例外的原因是整数不包含有理数(= 基本上是分数)。因此,尝试解析0.3为 int 是无稽之谈。Adoublefloat数据类型可以保存有理数。

The way Java casts a doubleto an intis done by removing the part after the decimal separator by rounding towards zero.

Java 将 a 转换double为 an 的方式int通过向零舍入删除小数点分隔符之后的部分

int i = (int) 0.9999;

iwill 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.

这样你就可以得到你试图得到它的价值的答案。