Home Java Integer Methods doubleValue() Java Integer doubleValue() Method Definition In Java, boxed.doubleValue() returns the number inside an Integer as a primitive double. It is the usual way to start a calculation that needs a decimal answer.
Every int fits exactly in a double, so the conversion never loses precision. The result prints with a decimal part, such as 42.0.
An instance method of java.lang.Integer: call it on an Integer object. Integer lives in java.lang, so no import is needed.
Syntax Call it on an Integer. It takes no arguments.
Integer total = 17;
Integer count = 4;
int whole = total / count; // 4 (integer division)
double exact = total.doubleValue() / count; // 4.25 Call What it does boxed.doubleValue()The value as a double. (double) boxedA cast has the same effect on an Integer. a.doubleValue() / bDivision that keeps the decimal part. Math.round(x * 10) / 10.0Rounds a result to one decimal place.
Convert before dividing. If both operands are integers, Java divides as integers and throws the decimal part away.
Library declarations // java.lang.Integer
public double doubleValue() Parameters Parameter Description None doubleValue() takes no parameters.
Return Value Return Value Description doubleThe same number as a 64-bit double. It is exact for every Integer.
Important
The classic bug is total / count with two integers: it returns 4 for 17 / 4. Convert one side first, as in total.doubleValue() / count. A double prints with a trailing .0 for whole numbers, and large values print in scientific notation, such as 2.147483647E9 for Integer.MAX_VALUE. Dividing by zero in double arithmetic gives Infinity or a not-a-number result instead of an exception.
Compare with Similar Methods Related conversions:
Method How it differs floatValue()A smaller type: it cannot hold every int exactly. longValue()Widens to long, still a whole number. intValue()Unwraps back to a plain int. parseInt(s)Reads text into an int before you convert it.
Java Examples 1. Integer division fixed 2. Percent and rounding 3. Exact for every int Example 1 — Calculate an Average with a Decimal Part Divide two Integer values with and without doubleValue().
public class IntegerDoubleValueDemo {
public static void main(String[] args) {
Integer total = 17;
Integer count = 4;
System.out.println("integer division: " + (total / count));
System.out.println("with doubleValue: " + (total.doubleValue() / count));
System.out.println("converted value: " + total.doubleValue());
}
} integer division: 4
with doubleValue: 4.25
converted value: 17.0 How It Works Two integers divide as integers. 17 divided by 4 is 4 with a remainder of 1, and Java keeps only the 4.Convert one side. Once the numerator is a double, the whole division is done in decimal arithmetic.The real average. 17.0 divided by 4 is 4.25.A whole number still shows a point. total.doubleValue() prints as 17.0, which marks it as a double.Example 2 — Work Out a Percentage and Round It Turn a score into a percentage with doubleValue() and round it to one decimal place.
public class IntegerDoubleValuePercent {
public static void main(String[] args) {
Integer correct = 37;
Integer total = 50;
double percent = correct.doubleValue() / total * 100;
System.out.println("percent: " + percent);
Integer passed = 2;
Integer all = 3;
double raw = passed.doubleValue() / all * 100;
double rounded = Math.round(raw * 10) / 10.0;
System.out.println("rounded: " + rounded);
System.out.println("wrong version: " + (passed / all * 100));
}
} percent: 74.0
rounded: 66.7
wrong version: 0 How It Works Decimal first, then scale. 37.0 divided by 50 is 0.74, and times 100 gives 74.0.A repeating decimal. 2 out of 3 is 66.666..., which Math.round(raw * 10) turns into 667.Divide by 10.0. Dividing by a double brings the rounded value back to 66.7.The integer version is wrong. passed / all is 2 / 3, which is 0 in integer arithmetic, so the percentage prints as 0.Example 3 — See That Every int Fits in a double Convert the extremes of int to double and back, and see how large values are printed.
public class IntegerDoubleValueExact {
public static void main(String[] args) {
Integer max = Integer.MAX_VALUE;
Integer min = Integer.MIN_VALUE;
System.out.println("max as double: " + max.doubleValue());
System.out.println("min as double: " + min.doubleValue());
System.out.println("back to int: " + (int) max.doubleValue());
System.out.println("round trip ok: " + ((int) min.doubleValue() == min));
System.out.println("million: " + Integer.valueOf(1000000).doubleValue());
}
} max as double: 2.147483647E9
min as double: -2.147483648E9
back to int: 2147483647
round trip ok: true
million: 1000000.0 How It Works Scientific notation. Doubles of 10000000 or more print as a number between 1 and 10 times a power of ten, so MAX_VALUE shows as 2.147483647E9.The sign is kept. MIN_VALUE prints as -2.147483648E9.No precision is lost. A double has 53 bits of precision, more than the 32 an int needs, so converting back gives the same number.Smaller values print plainly. One million is below 10000000, so it prints as 1000000.0.Frequently Asked Questions What does Integer.doubleValue() do? It returns the Integer as a primitive double. For example, an Integer holding 42 gives 42.0.
How do I get a decimal result when dividing two Integer values? Convert one operand first: a.doubleValue() / b. If both are integers, Java divides as integers and drops the decimal part.
Does doubleValue() lose precision? No. Every int fits exactly in a double, so converting back with a cast returns the same number.
Why does the output show E9 for large values? Doubles of 10000000 or more are printed in scientific notation, so 2147483647 prints as 2.147483647E9. Use String.format("%.0f", x) to avoid it.
What is the difference between doubleValue() and a cast? For an Integer they give the same result. The cast (double) boxed unboxes and widens in one step.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion