Вы находитесь на странице: 1из 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

25

Convert Double to Int, rounded down


java

type-conversion

rounding

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.

share

improve this question


Vogatsu
136 1 2 8

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

improve this answer


waqaslam
42.3k 7 92 117

Answered
Apr 23 '12 at 12:29

Edited
Jan 26 '15 at 10:52

Thanks, solved my problem Vogatsu Apr 23 '12 at 12:40


What does it mean to "loose decimal values"? Does it give the same numbers as calling Math.floor()? Samuel Edwin Ward Dec 27 '15 at 0:17
add a comment

20

If the double is a Double with capital D (a boxed primitive value):


Double d = 4.97542;
int i = (int) d.doubleValue();
// or directly:
int i2 = d.intValue();

If the double is already a primitive double, then you simply cast it:
double d = 4.97542;
int i = (int) d;

share

improve this answer


Natix
6,854 5 29 54

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

double myDouble = 420.5;


//Type cast double to int
int i = (int)myDouble;
System.out.println(i);

The double value is 420.5 and the application prints out the integer value of 420

share

improve this answer


Habib
140k 16 200 251

share

Another option either using Double or double is use Double.valueOf(double d).intValue();. Simple and clean

improve this answer


Joao Evangelista
732 1 6 23

Answered
Apr 23 '12 at 12:32

I think I had a better output, especially for a double datatype sorting.


Though this question has been marked answered, perhaps this will help someone else;
Arrays.sort(newTag, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final Integer time1 = (int)Integer.valueOf((int) Double.parseDouble(entry1[2]));
final Integer time2 = (int)Integer.valueOf((int) Double.parseDouble(entry2[2]));
return time1.compareTo(time2);
}
});

Answered
Nov 20 '15 at 13:46

share

improve this answer


HexxonDiv
3 1 6

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

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Вам также может понравиться