Public Group

# rounding to 1 decimal place in Java

This topic is 3652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i've been looking through the documentation and I can't find a method to round a double to 1 decimal place, does anyone know of one? i wa looking through the Math class and couldn't find one thanks

##### Share on other sites
Quote:
 Original post by godsenddeathi've been looking through the documentation and I can't find a method to round a double to 1 decimal place, does anyone know of one? i wa looking through the Math class and couldn't find onethanks

Here's a hint.

What is 3.16 * 10?
What do you get when you round that off to the nearest integer?
What is 3.2 * 10?

##### Share on other sites
sorry i must be slow, but i have no idea how that would work, there's no number that you can multiple by 23.2897235987345786234723 to get 23.3

##### Share on other sites
Quote:
 Original post by godsenddeathsorry i must be slow, but i have no idea how that would work, there's no number that you can multiple by 23.2897235987345786234723 to get 23.3
A quick way to implement rounding (up or down) is to add 0.5 before casting to an integer. e.g.
 23.28972 * 10=232.8972 232.8972 + 0.5=233.3972 233.3972 -> integer=233 233 / 10.0=23.3

##### Share on other sites
oh ok, that makes a bit more sense, i looked it up and found a function to do it, thanks guys

##### Share on other sites
Quote:
 Original post by godsenddeathI can't find a method to round a double to 1 decimal place

You may find solutions that seem to work fine, but in reality, you CANNOT round a double to 1 decimal place, because a double is represented as a binary fraction, not a decimal fraction. There are no decimal places in a double. Most finite decimal fractions like 0.1 CANNOT be represented exactly with finite binary fractions.

Try this:
import java.math.BigDecimal;public class RoundTest{	public static void main(String[] args)	{		double x = 0.72;		System.out.println(x);		x = Math.rint(10 * x) / 10;		System.out.println(x);		System.out.println(new BigDecimal(x));	}}

Now System.out.println(double) formats the binary fraction as decimal, so a human can read it, but it doesn't reveal the whole truth. You only get that by creating a BigDecimal, and that truth looks like this:

0.6999999999999999555910790149937383830547332763671875

This is the closest you can get to 0.7 with double.

##### Share on other sites
However, 0.6999999999999999555910790149937383830547332763671875 is close enough to 0.7 (Read more about how floats are stored in computing)

Even then, you have up to 16 significant figures to work with which is good enough in most cases. If you want better accuracy, with a sacrifice of performance, read up on decimal floating point.

##### Share on other sites
yep, i'm aware, one of the first topics in my CS class

##### Share on other sites
Quote:
 Original post by godsenddeathsorry i must be slow, but i have no idea how that would work, there's no number that you can multiple by 23.2897235987345786234723 to get 23.3

I didn't suggest anything of the sort. Let's see what happens if we attempt to answer my questions:

Quote:
 What is 3.16 * 10?

31.6, of course.

Quote:
 What do you get when you round that off to the nearest integer?

32.

Quote:
 What is 3.2 * 10?

32.

Hmm... so what's going on here? When we multiply our input by 10 and round it to the nearest integer, we get the same thing as when we take our expected result, and multiply it by 10. Can you think of why this might be?

Now, what is the logically implied process? Well, we have a process that gives us the same thing as "taking our expected result and multiplying by 10". That is, we have a process that gives us 10 times the expected result. Thus, we can get the expected result by dividing by 10. That process was "multiply by 10, then round to the nearest integer"; thus, the whole procedure that we want is "multiply by 10, round to the nearest integer, and divide by 10 again". Now, dividing an integer by an integer will drop the remainder, so we want the rounded value to be a floating-point number before we do the division. Fortunately, we have an easy way to get that: Math.rint().

I'm sorry to have to say anything negative, but being a programmer pretty much requires you to be able to figure out this sort of thing for yourself. It's all problem-solving. More experienced folk will often give you "hints" like this because they want you to actually understand what is going on and not blindly copy and paste something.

##### Share on other sites
By the way, if you just want to round for representation on the screen, there's a much easier solution:
double x = 0.72;System.out.printf("%.1f\n", x);

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632578
• Total Posts
3007158

×