How to Round to 2 Decimal Places in Java
Java can store some fairly big numbers. This precision is welcomed by many, but not always for the end user. This lesson will show you how to round to two decimal places using working code.
Complex Numbers
If you use Java to perform currency calculations or even divide complex numbers, you’re going to end up with numbers that are long and difficult to visualize for an end user. For example, what can I do with the 0.90877 Euros I get for one American dollar?
Before we begin rounding in Java, let’s look at a few lines of code that result in a complex number, like these:
double base = 50.6854;
double rate = .90877;
double howMuch = base * rate;
System.out.println(howMuch);
When this code is run, the following output is displayed:
Java complex number
Rounding in Java: Two Options
There are a couple of ways to achieve the rounding. Both are methods that Java provides. The first will require a little modification to its parameters, but it is a workable solution. Let’s first take a look at the rounding feature of Java.
The Java Math class includes Math.round, which is a method for rounding numbers that takes a single parameter, the number being rounded. However, if we use it as it is, it will simply round our number to the nearest whole number. This code uses Math.round to round the base * rate.
double base = 50.6854;
double rate = .90877;
System.out.println(Math.round(base * rate));
When the code is executed, the output looks like this:
Java math.round basic output
We are close, but we need to display two decimal places. By tweaking the input parameter slightly, we can use some creative math to give us the two decimal places. Because the Math.round function rounds to the nearest whole number, we will first multiply the base * rate by 100.0. The .0 tells Java we intend to deal with a float or double value. Next, we divide that value by 100.0, which shifts the decimal points two places to the left, giving us 46.06.
When it’s run, this output is displayed:
Java Math.round output
While this accomplishes what we’re trying to do, there is another method for rounding to two decimal places, the DecimalFormat method. The DecimalFormat method is actually in the text utility of Java, which means we have to import that utility before the main method of our program, like this:
import java.text.DecimalFormat;
DecimalFormat is a class in Java, so we will need to create a new instance of that class. When the class is created, we pass along the format we would like to use, like this:
DecimalFormat format = new DecimalFormat(“##.00”);
The pound signs indicate the formatting of the number before (to the left of) the decimal point. You could enter ###,###,###,###.00 for very large numbers.
Now that the instance of the DecimalFormat class has been declared, we can display the result of our math. The DecimalFormat class has a format method that we need to call. There’s nothing special there since we already told it how we want the number formatted. The format method is called within the display of the output, like this:
double base = 50.6854;
double rate = .90877;
double howMuch = base * rate;
DecimalFormat format = new DecimalFormat(“##.00”);
System.out.println(Math.round(format.format(howMuch));
To unlock this lesson you must be a Study.com Member. Create your account
Additional Activities
Rounding Decimals in Java: True or False Activity
This activity will help assess your knowledge in rounding up to two decimal places in Java, as presented in the lesson.
Directions
Based on the given code, determine whether the following statements are true or false. To do this, print or copy this page on a blank paper and underline or circle the answer.
1. The code rounds the area of a circle into two decimal places. True | False
2. The value of PI in Java is 3.141592653589793, which is an example of a complex number. True | False
3. Running this code will display a single method in rounding decimal places. True | False
4. The two methods presented in this code are the Math.round and DecimalFormat method. True | False
5. In Java, the Math.round method is used for rounding numbers that take a single parameter. True | False
6. DecimalFormat method runs directly without the need to import some text utility. True | False
7. To round the area to two decimal places, lines 9 and 13 should be corrected. True | False
8. In line 12, an additional zero should be placed after the pound sign to round the result to two decimal places. True | False
9. The DecimalFormat may be removed to avoid the overflow of your data. True | False
10. In line 9, the correct format for 100 should be 100.0 to round the result to two decimal places. True | False
Answer Key
False, because the correct statement is: The code rounds the area of a circle into one decimal place.
True
False, because the correct statement is: Running this code will display two methods in rounding off decimal places.
True
True
False, because the correct statement is: DecimalFormat method runs directly by importing some text utility, as in line 3 of the code.
False, because the correct statement is: To round the area to two decimal places, lines 9 and 12 should be corrected.
True
False, because the correct statement is: The Math.round method may be removed to avoid the overflow of your data.
True