-3

This is the code i am using to calculate a percentage and round it to 2 decimal places. However, at the moment, the result comes out as 45.0% rather than 45.33%

int one = 432;
int rolls = 953;

double test1 = 100 * one / rolls;
double finalValue1 = Math.round( test1 * 100.0 ) / 100.0;

Why are no decimal places showing?

Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
  • https://stackoverflow.com/questions/2538787/how-to-display-an-output-of-float-data-with-2-decimal-places-in-java – awksp May 01 '14 at 05:30
  • The reason no decimal places are showing is because I'm fairly certain that Java will print the least number of decimals needed to express the `double`. In this case, 45.00 = 45.0, so Java will skip the extra decimal place and print the latter. – awksp May 01 '14 at 05:31
  • Yesterday, the **exact same question** was asked. Is it "round to 2 decimal places in Java assignment" time this week or what? – The Paramagnetic Croissant May 01 '14 at 05:34
  • possible duplicate of [Java how to divide 2 doubles with 2 digits precision?](http://stackoverflow.com/questions/23385867/java-how-to-divide-2-doubles-with-2-digits-precision) – The Paramagnetic Croissant May 01 '14 at 05:35
  • I would argue that this is not a duplicate question. Yes if you look at just the title it is a duplicate question, but if you look at his question and his code, you'll notice that this is actually a precision error and not a formatting error. And this duplicate error is specific to this very case. OP should be guided to edit the title in his question. I've made an edit to the title. – Tamby Kojak May 01 '14 at 05:45

4 Answers4

1

as you are multiplying integers the result of test1 is integer
so you have to say
double test1= 100.0*one/rolls; or
double test1=(double)100*one/rolls

monim
  • 3,427
  • 3
  • 23
  • 36
0

You could use String.format("%.2f",test1). If you use round, then java would round the the integer value. Thus, formatting it this way would give you the answer you seek.

0

The problem is with the following line:

double test1 = 100 * one / rolls;

That is because 100, one, and rolls are of type int.

When you do 100 * one, that will result in 43200. Then, we have to execute the rest of the calculation... 43200 / 953 would actually equal 45.330535152, but because these are both of type int the result will be 45. Making test1 = 45. Since test one is a double, it will actually be 45.0.

The next calculation, which uses test1 will be off because of the above, resulting in "no decimal value".

To fix this, you can change the type of one and rolls to double.

Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
-2

You have two problems.

First and foremost, you are performing integer division

double test1 = 100 * one / rolls;

100, one and rolls are all int. This means the result is an integer, regardless of what you've declared the return type to be. This is covered in the SO question Why the result of 1/3=0 in java?:

the result variable as double just causes an implicit conversion to occur after division.

If you want doubles, use doubles:

double one = 432.0;
double rolls = 953.0;

After fixing that, your division of Math.round( test1 * 100.0 ) / 100.0; will produce a double, but probably with more than two places of precision. It's unclear at that point if you want further rounding to a specific precision, but if you only wanted to print the two digits after the decimal you could use:

System.out.printf("%.2f", finalValue1);
Community
  • 1
  • 1
Brian Roach
  • 76,169
  • 12
  • 136
  • 161