Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert double to Int, rounded down

How to convert a double value to int doing the following:

Double If x = 4.97542. Convert to int x = 4.  Double If x = 4.23544. Convert to int x = 4. 

That is, the answer is always rounding down.

like image 324
Vogatsu Avatar asked Apr 23 '12 12:04

Vogatsu


People also ask

Does double to int round down?

That is, the answer is always rounding down.

How do you make an int round down?

To round up to the nearest specified place, use the ROUNDUP function. To round up to the nearest specified multiple, use the CEILING function. To round down and return an integer only, use the INT function. To truncate decimal places, use the TRUNC function.

Does int () round up or down?

However, INT actually is more sophisticated than that. INT rounds a number down using the Order rounding method. That is, it rounds a positive number down, towards zero, and a negative number down, away from zero. Therefore, it's easy to use INT to round a number up using the Math method.


1 Answers

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.

like image 183
waqaslam Avatar answered Oct 13 '22 08:10

waqaslam