Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Double to int directly? [duplicate]

Tags:

java

May be this is silly question. I want to get rid of the fractional part of the Double number. But I cant do that. It shows the error that incompatible types. What to do?

Double to int conversion in one line....please help thanks

like image 721
Selvin Avatar asked Mar 23 '11 10:03

Selvin


People also ask

Can I convert a double to integer?

round() method converts the double to an integer by rounding off the number to the nearest integer. For example – 10.6 will be converted to 11 using Math. round() method and 1ill be converted to 10 using typecasting or Double. intValue() method.

Can double be converted to int java?

Answer: In Java, the primitive data type double can be converted to primitive data type int using the following Java class methods and ways: typecasting: typecast to int. Math. round()


1 Answers

If you really should use Double instead of double you even can get the int Value of Double by calling:

Double d = new Double(1.23); int i = d.intValue(); 

Else its already described by Peter Lawreys answer.

like image 83
crusam Avatar answered Oct 07 '22 01:10

crusam