# Thread: round to two decimal places

1. Member
Join Date
Nov 2007
Posts
5
Rep Power
0

## round to two decimal places

is there an easy way to round a double to two decimal places?

i.e. 1.98999 would round to 1.99

the math.round method only rounds to nearest integer.

decimalformat class converts number to a string.

i figure there must be an easy way to do this...?? pls advise.

2. Java Code:
```double[] d = { 1.0/3.0, Math.PI, 2.0/3.0 };
for(int j = 0; j < d.length; j++) {
System.out.printf("d[%d] = %f%n", j, d[j]);
double d2 = (int)Math.round(d[j] * 100)/100.0;
System.out.printf("d2   = %f%n", d2);
}```

3. Member
Join Date
Dec 2011
Posts
1
Rep Power
0

## Re: round to two decimal places

Simple solution:

(Math.round(d*100))/100
Last edited by HansW; 12-23-2011 at 04:30 PM.

4. ## Re: round to two decimal places

Originally Posted by HansW
Simple solution:
Wrong. Not a solution at all.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•