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.
That is, the answer is always rounding 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With