Core Java · Chapter 35 of 48
BigDecimal and money
By the end of this chapter you can add, multiply, round and divide money exactly with BigDecimal, so that every paisa on a bill adds up. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Core Java
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Ravi has a till at NDLS that does not add up. Three kesar chai at ₹40.10 show as 120.30000000000001, the day's GST is a few paise off the accountant's sheet, and a group of students splitting a bill paid two paise less than it. Meera: "Thirty counters, a thousand bills a day each. Paise matter."
So far you have kept prices in whole rupees as int, which cannot hold ₹40.10 at all, or you would reach for double. A double stores numbers in base 2, and most decimal prices have no exact base 2 form, just as 1/3 has no exact decimal form:
System.out.println(0.1 + 0.2);
System.out.println(40.10 * 3);
0.30000000000000004
120.30000000000001What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Bill.java: turn a double price into the decimal the person meant
In Bill.java, make fromDouble(price) return the price as a BigDecimal with the digits a person would have typed, the way Java writes the double as text, not the double's binary approximation. The check compares with equals, so the scale matters too: 40.5 must give 40.5, not 40.50.
For example, fromDouble(0.1) returns 0.1 (not 0.1000000000000000055511151231257827021181583404541015625), fromDouble(12.35) returns 12.35 and fromDouble(40.5) returns 40.5.
- Task 2
Bill.java: add up the bill exactly, price times quantity
In Bill.java, make subtotal() return the sum, over every line of the bill, of the line's price times its quantity, as an exact BigDecimal with no rounding. An empty bill returns zero.
For example, 2 x 40.00 and 1 x 50.00 give 130.00; 0.10 and 0.20 give 0.30 (with doubles, 0.30000000000000004); 3 x 40.10 gives 120.30; an empty bill gives 0.
- Task 3
Bill.java: add 5% GST, rounded half up to the paisa
In Bill.java, make gst() return 5% of the subtotal (GST_RATE), rounded half up to exactly two decimal places, and total() return the subtotal plus the GST, also with exactly two decimal places. The check compares with equals, so 6.5000 or 6.5 where 6.50 is expected fails. An empty bill's total is 0.00.
For example, a subtotal of 130.00 gives a GST of 6.50 and a total of 136.50; 12.50 gives a GST of 0.63 (0.625 rounds up) and a total of 13.13; 3 cutting chai at 25 give 3.75 and 78.75.
- Task 4
Bill.java: split the total so the shares add back up
In Bill.java, make split(people) return a list of people shares of total(), each with exactly two decimal places. The shares differ by at most one paisa, the larger ones come first, and they add up to the total exactly. When people is less than 1, throw an IllegalArgumentException.
For example, 136.50 split 4 ways gives [34.13, 34.13, 34.12, 34.12]; split 3 ways gives [45.50, 45.50, 45.50]; split 1 way gives [136.50]; 13.13 split 3 ways gives [4.38, 4.38, 4.37]. split(0) throws.
- Task 5 · interview question
Interview: Bill.java, predict equals and compareTo on 2.0 and 2.00
In Bill.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case, numbers as digits).
The check runs the same code and compares your list with what Java really printed.
What you will learn
- BigDecimal and its scale: an exact decimal, and the number of digits after the point.
- Making one: from a String, with valueOf, and the constructor to avoid.
- Arithmetic and rounding: add, multiply, setScale and divide, with a RoundingMode.
- equals against compareTo: two ways to ask "the same?".
More in Core Java
Build "BigDecimal and money" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.