Академический Документы
Профессиональный Документы
Культура Документы
Stack Overflow
log in
25
type-conversion
rounding
share
Asked
Apr 23 '12 at 12:27
Bart
13.5k 6 31 45
Edited
Feb 17 '13 at 17:28
5 Answers
58
Order By
If you explicitly cast double to int, it will loose decimal values. 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.
Votes
share
Answered
Apr 23 '12 at 12:29
Edited
Jan 26 '15 at 10:52
20
If the double is already a primitive double, then you simply cast it:
double d = 4.97542;
int i = (int) d;
share
Answered
Apr 23 '12 at 12:34
Edited
May 6 '13 at 21:31
can you please tell me what is the difference between Double and double ? Muhammed Refaat Apr 27 '14 at 7:48
a double is a primitive float point value. A Double is a Java Object. penguin Nov 3 '15 at 21:23
add a comment
The double value is 420.5 and the application prints out the integer value of 420
share
share
Another option either using Double or double is use Double.valueOf(double d).intValue();. Simple and clean
Answered
Apr 23 '12 at 12:32
Answered
Nov 20 '15 at 13:46
share
Answered
Nov 7 '15 at 21:29
Suseika
3,289 11 27 56
Edited
Nov 7 '15 at 21:57
Your Answer
log in
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc