Rabu, 28 Juli 2021

How to convert double to int in Java? Example

Suppose you have a double primitive variable 4.444 and you want to convert it to the integer value 4, how do you that in Java? Since double is bigger data type than int, you can simply downcast double to int in Java. double is 64-bit primitive value and when you cast it to a 32-bit integer, anything after the decimal point is lost. Btw, typecasting doesn't do any rounding or flooring, which means if you have 9.999999 and while casting to int you are expecting 10 then you would be disappointed, casting will give you just 9. If you need 10 then you need to use Math.round() method to first round the double value to the nearest integer and then truncate decimals.

Lorem ipsum is simply dummy text of the printing and typesetting industry.

Comments


EmoticonEmoticon