在java中将double转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6468730/
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-16 06:12:16  来源:igfitidea点击:

converting double to integer in java

javacastingdoublerounding

提问by vdMandele

In Java, I want to convert a double to an integer, I know if you do this:

在Java中,我想将double转换为整数,我知道你是否这样做:

double x = 1.5;
int y = (int)x;

you get y=1. If you do this:

你得到 y=1。如果你这样做:

int y = (int)Math.round(x);

You'll likely get 2. However, I am wondering: since double representations of integers sometimes look like 1.9999999998 or something, is there a possibility that casting a double created via Math.round() will still result in a truncated down number, rather than the rounded number we are looking for (i.e.: 1 instead of 2 in the code as represented) ?

你可能会得到 2。但是,我想知道:由于整数的双重表示有时看起来像 1.9999999998 或其他东西,是否有可能通过 Math.round() 创建的双重转换仍然会导致被截断的数字,而不是比我们正在寻找的四舍五入数字(即:代码中的 1 而不是 2 表示)?

(and yes, I do mean it as such: Is there anyvalue for x, where y will show a result that is a truncated rather than a rounded representation of x?)

(是的,我的意思是这样的:x是否有任何值,其中 y 将显示 x 的截断而不是舍入表示的结果?)

If so: Is there a better way to make a double into a rounded int without running the risk of truncation?

如果是这样:是否有更好的方法将 double 转换为舍入 int 而不冒截断的风险?



Figured something: Math.round(x) returns a long, not a double. Hence: it is impossible for Math.round() to return a number looking like 3.9999998. Therefore, int(Math.round()) will never need to truncate anything and will always work.

想出一些东西:Math.round(x) 返回一个 long,而不是一个 double。因此: Math.round() 不可能返回一个看起来像 3.9999998 的数字。因此, int(Math.round()) 将永远不需要截断任何内容并且将始终有效。

采纳答案by jjnguy

is there a possibility that casting a double created via Math.round()will still result in a truncated down number

是否有可能铸造一个双重创建的通过Math.round()仍然会导致一个被截断的数字

No, round()will always round your double to the correct value, and then, it will be cast to an longwhich will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.

不,round()将始终将您的双精度舍入到正确的值,然后,它将被转换为long将截断任何小数位的an 。但四舍五入后,将不会有任何小数部分剩余。

Here are the docs from Math.round(double):

以下是来自的文档Math.round(double)

Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

返回最接近参数的 long。通过加 1/2、取结果的下限并将结果转换为 long 类型,结果四舍五入为整数。换句话说,结果等于表达式的值:

(long)Math.floor(a + 0.5d)

回答by Charlie

For the datatype Doubleto int, you can use the following:

对于数据类型Doubleto int,您可以使用以下内容:

Double double = 5.00;

int integer = double.intValue();

回答by Nandkishor Gokhe

Double perValue = 96.57;
int roundVal= (int) Math.round(perValue);

Solved my purpose.

解决了我的目的。