Simple split equation in java

3

Good afternoon, I'm a beginner in Java and I need to do the following calculation in the language

For example: (8/7) * 100 = 114%

(4/7) * 100 = 57.14%

(90/112) * 100 = 80.35%

But the way I'm developing it does not return the correct result.

  double total = (90/112) * 100 ;
  System.out.println(total);

and it returns me 0.0 in the console

How would I get the actual result back?

    
asked by anonymous 13.06.2016 / 21:04

2 answers

8

( 90/112 ) is a division of integers.

You can cast at least one of the operators to get the desired result:

double total = ((double) 90 / 112) * 100;
System.out.println(total); // 80.35714285714286

Other ways to cast:

double total = (90d / 112) * 100;
double total = (90.0 / 112) * 100;
    
13.06.2016 / 21:16
3

There are two ways to resolve this:

double total = (90/112d) * 100 ;
System.out.println(total);

Or:

double total = (90./112.) * 100 ;
System.out.println(total);
    
13.06.2016 / 22:12