Calculate increase or decrease percentage in Java
Contents
What is percentage?
In mathematics , Percentage is a number or ratio expressed as a fraction of 100. If we calculate the part of whole number and multiply the result by 100, we will get the percentage value. It is denoted by percent (%) sign.
Percentage = (part/whole) x 100
Example : A student scored 120 marks out of 150 and the percentage is calculated as (120/150) * 100 = 80 %
Increase or Decrease percentage
In some cases, we want to compare the previous value and current value to check whether it is increased or decreased. Lets look at the formula to calculate this difference in the form of percentage.
Increase or decrease percentage = (New value – Old value) / Old value x 100
If the new value is greater than the old value, we will get the the increase percentage as a positive number.
If the new value is lesser than the old value, we will get the decrease percentage as a negative number.
Java program to calculate the increase or decrease percentage
Lets consider Stock market as a example where each stock price is changed every day. The stock market using the percentage notation to show whether the particular stock is increased or decreased. If we compare the stock price between the previous day and current day, we can get that percentage value.
In this example, we are going to write a java program to calculate the increase or decrease percentage value for stocks. This is a command line program in which user needs to provide the input for yesterday and todays stock price. Based on these values, we are calculating the percentage value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import java.text.DecimalFormat; import java.util.Scanner; public class Percentage { //Round percentage value to 2 decimal places private static DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { float yesterdayPrice, todayPrice,priceDifference = 0; float changeInPercentage = 0; try{ // Get user input using Scanner class System.out.println("Enter Yesterday Stock Price : "); Scanner input = new Scanner(System.in); yesterdayPrice = input.nextFloat(); System.out.println("Enter Today's Stock Price : "); input = new Scanner(System.in); todayPrice = input.nextFloat(); if(yesterdayPrice > 0 && todayPrice > 0 && todayPrice > yesterdayPrice){ // Increase percentage => today price is higher than yesterday price priceDifference = todayPrice - yesterdayPrice ; changeInPercentage = priceDifference / yesterdayPrice * 100; }else if(yesterdayPrice > 0 && todayPrice > 0 && todayPrice < yesterdayPrice){ // Decrease percentage => today price is lesser than yesterday price priceDifference = todayPrice - yesterdayPrice; changeInPercentage = priceDifference / yesterdayPrice * 100; }else if(yesterdayPrice > 0 && todayPrice == 0){ // Decrease percentage => today price is lesser than yesterday price priceDifference = todayPrice - yesterdayPrice; changeInPercentage = priceDifference / yesterdayPrice* 100; }else if(yesterdayPrice == 0 && todayPrice > 0){ // Increase percentage => today price is greater than yesterday price priceDifference = todayPrice - yesterdayPrice; // since yesterday price is zero, it is 100 % increase for today // so dividing the difference by today price changeInPercentage = priceDifference / todayPrice * 100; }else if( yesterdayPrice ==0 && todayPrice == 0){ priceDifference = 0; changeInPercentage = 0 * 100; }else{ System.out.println("Invalid value to calculate percentage"); } System.out.println("Price Difference : " + priceDifference); System.out.println("Percentage Growth : " + df.format(changeInPercentage) + "%"); }catch (Exception e){ System.out.println("Error occurred due to invalid value : " + e); } } } |
Output
Enter Yesterday Stock Price :
75
Enter Today’s Stock Price :
105
Price Difference : 30.0
Percentage Growth : 40.00%