Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a question about percentage change in java

I need to find the percentage change of different numbers, I’ve got the formula down but the result keep ending up being minus instead of a positive e.g. 100 to 150 comes out a -50.00% instead of 50.00%. Thanks in advance!

package package2;
import java.util.Scanner;

class Selection3 {

public static void main (String [] args)
{

    perChange();

    
}


public static void perChange() {
    double perCha0, perCha1, perCha2, perCha3, perCha4, perCha5;


    perCha0 = ((38 - 108)*100/38);
    perCha1 = ((35 - 63)*100/35);
    perCha2 = ((4 - 6)*100/4);
    perCha3 = ((3 - 5)*100/3);
    perCha4 = ((20 - 40)*100/20);
    perCha5 = ((100 - 150)*100/100);
    
    System.out.println(perCha0);
    System.out.println(perCha1);
    System.out.println(perCha2);
    System.out.println(perCha3);
    System.out.println(perCha4);
    System.out.println(perCha5);
    
}

output

-184.0
-80.0
-50.0
-66.0
-100.0
-50.0

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem you have is a math problem, and you have encoded a bad formula into your programming.

A percentage of change is a "difference of change" divided by the original amount. When 30 items become 60 items, it is a difference of +30; but, because you subtract the numbers in the wrong order, you get an incorrect "difference" of -30.

Subtracting is not like addition, changing the order of the numbers doesn’t result in the same result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading