Decimal numbers: double and float
16 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A decimal number has a decimal point, like 92.5, 15999.99 or 9.8. Java stores decimal numbers in double, or in the smaller float.
double: the usual decimal type
Your attendance is 92.5% and a samosa costs 15.5 rupees, but an int cannot hold the .5. Use double, the usual decimal type: every decimal literal like 92.5 is a double.
Example 1 · Whole numbers in a double
public class Bill { public static void main(String[] args) { double cash = 500; double samosa = 15.5; double chai = 12.0; double bill = samosa + chai; System.out.println("Cash: " + cash); System.out.println("Bill: " + bill); }}Cash: 500.0 Bill: 27.5
What this code does
double cash = 500;stores the whole number 500 as500.0, soprintlnprints500.0.+adds the two prices: 15.5 + 12.0 is 27.5.
A double keeps about 15–16 significant digits (the digits that carry the value, counted from the first non-zero digit). That is plenty for marks, prices and measurements.
float needs an f
Java has a smaller decimal type, float: half the memory of a double, but only about 6–7 significant digits. 4.3 is a double, so float rating = 4.3; does not compile; add f: 4.3f.
Example 2 · float and double side by side
public class Prices { public static void main(String[] args) { double phonePrice = 15999.99; float rating = 4.3f; float exact = 1234567.89f; // too many digits for a float double moreExact = 1234567.89; System.out.println("Phone price: " + phonePrice); System.out.println("Rating: " + rating); System.out.println("As float: " + exact); System.out.println("As double: " + moreExact); }}Phone price: 15999.99 Rating: 4.3 As float: 1234567.9 As double: 1234567.89
What this code does
ratingis afloat, so its literal ends withf. Thefis not printed.- Without the
f, the error ispossible lossy conversion from double to float. Lossy means some detail may be lost. - 1234567.89 has 9 significant digits. A
floatkeeps only about 7, so it prints1234567.9; thedoublekeeps them all.
double unless you have a reason to use float.Decimals are not always exact
Add 0.1 and 0.2 and Java prints 0.30000000000000004, not 0.3. Many decimals, like 0.1, cannot be stored exactly in 0s and 1s, just as 1/3 cannot be written exactly as 0.333…, so Java keeps a very close value.
public class Sums { public static void main(String[] args) { System.out.println(0.1 + 0.2); System.out.println(0.5 + 0.25); }}0.30000000000000004 0.75
This is not a Java bug; most languages do the same. 0.5 + 0.25 comes out clean because both have an exact form. Try other values in the lab: some sums come out clean, some do not, and a float shows fewer digits.
For real money that must be exact, use BigDecimal (a class a later module covers) or store whole paise in a long. In these lessons we use double to practise decimals.
Experiment
Precision lab
Change a and b. Compare the double sum with the same sum kept as a float.
- a + b as a double
- 0.30000000000000004
- a + b as a float
- 0.3
- a + b + b
- 0.5
Next step
Check your understanding
8 quick questions