Copyright

How to Round to 2 Decimal Places in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Round to One Decimal Place in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Complex Numbers
  • 0:33 Rounding in Java: Two Options
  • 2:53 Comparing the Two Options
  • 4:15 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

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

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support