Convert Double to Int

Converting double to integer in Java

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

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

Here are the docs from 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)

convert double to int

You can use a cast if you want the default truncate-towards-zero behaviour. Alternatively, you might want to use Math.Ceiling, Math.Round, Math.Floor etc - although you'll still need a cast afterwards.

Don't forget that the range of int is much smaller than the range of double. A cast from double to int won't throw an exception if the value is outside the range of int in an unchecked context, whereas a call to Convert.ToInt32(double) will. The result of the cast (in an unchecked context) is explicitly undefined if the value is outside the range.

How to convert number from double to int without rounding?

You do it by casting to int, as you're already doing in the code you've presented as an attempt.

I understand that you feel unsatisfied because you think you have found a case when your attempt will round the value instead of dropping the decimal part.

You have tried with value 1.99999999999999999999 and you've noticed that casting it to int produces an int of value 2. You concluded from there that casting is not just dropping the decimal part, it is rounding to the closest whole number 2.

Your conclusion is incorrect. The number 2 is not obtained as a result of casting to int. The number 2 is a result of the compiler parsing the written literal value 1.99999999999999999999. doubles don't have infinite precision. That means you can't write as many decimals as you want and expect it to be correctly kept in a double value. doubles only offer approximations of what you're asking. So when you type the literal value 1.99999999999999999999, the compiler knows that it is incapable to represent that value exactly, and instead it will take it as the closest value that can be represented in a double.

And the representable value closest to 1.99999999999999999999 is 2. As far as the Java compiler is concerned, these two numbers are one and the same.

So when you write:

double d = 1.99999999999999999999d;

the Java compiler treats it completely equivalent to:

double d = 2d;

You'll notice that at this point, you have yet to do any attempt to drop the decimals and only keep the whole part. You've only declared a value for your double, and as far as you're concerned this value could very well have a decimal part.

Your attempt to only keep the whole value only happens when you do:

int i = (int)d;

and here the decimals are dropped and your int contains only the whole part of what the double value contained.

However, in your example, since your double value was 2.0, then taking the whole part of it is 2. That's not rounding. That's not anything else than keeping only the whole part.

The correct way to drop decimals and only keep the whole part, is to cast to int.

(If it is important to you to be able to manipulate values such as 1.99999999999999999999 and have them not be the same as 2.0, then you cannot use doubles. They don't have sufficient precision. You should use BigDecimal, with new BigDecimal("1.99999999999999999999"). The constructor must be called with a String rather than a floating-point value, since floating-point values are unable to represent the value you want.)

Convert double to Int, rounded down

If you explicitly cast double to int, the decimal part will be truncated. For example:

int x = (int) 4.97542;   //gives 4 only
int x = (int) 4.23544; //gives 4 only

Moreover, you may also use Math.floor() method to round values in case you want double value in return.

Converting double to int is giving unexpected results

TL;DR: (int)double always rounds towards zero. printf("%0.0lf", double) actually rounds to closest integer.


When you convert the double to int, then the result is rounded towards zero. E.g. (int)14.9999 is 14, not 15. Therefore your first example with pow() must have given you the result slightly below 15, therefore direct cast to int gives 14.

Now, when you use printf(), then other rules are used. Printf, when asked to print a double using smallest number of digits possible (%0.0lf) rounds it to the closest integer, therefore printf("%0.0lf", 14.666) prints 15.

Converting a double to an int in C#

Because Convert.ToInt32 rounds:

Return Value: rounded to the nearest 32-bit signed integer. If value
is halfway between two whole numbers, the even number is returned;
that is, 4.5 is converted to 4, and 5.5 is converted to 6.

...while the cast truncates:

When you convert from a double or float value to an integral type, the
value is truncated.

Update: See Jeppe Stig Nielsen's comment below for additional differences (which however do not come into play if score is a real number as is the case here).



Related Topics



Leave a reply



Submit