'How do I return the final discounted price instead of the discounted amount off?

I am creating a method that takes a percent as a parameter and decreases the price by that percent.

public void lowerPriceBy(double percent){
    price = price/100*percent;
}

How to I get my method to return the final discounted price of the car instead of the discounted amount?

For example if the car costs $10,000 and the percentage is 5% I want to return Price = $9500 but I'm only getting Price = $500 when I do the calculations.



Solution 1:[1]

You can get percent like this

Price= 10,000 Discount= 5%

Price after Discount= 10,000 - (10,000 * (5/100))

Answer will be= 9,500

Solution 2:[2]

It's a good thing to see your effort, but here are the issues with the question.

  1. Like they have noticed in the comments, your code is in Java and not Javascript
  2. As @Anuj Kumar pointed out, the logic of getting the Discounted Price is Price after Discount= 10,000 - (10,000 * (5/100))

With this implementation I also suggest making the method name specify what argument it accepts by changing it to lowerPriceByPercentage

Here is a working code of what you want to acheive:

public class Main{

    static double price;

    public static void main(String[] args) {
        price = 10000;
        lowerPriceByPercentage(5);
        System.out.println(price);
    }

    public static void lowerPriceByPercentage(double percent){
        price -= price*(percent/100);
    }
}

Run this online: https://onlinegdb.com/7V1Z33HbR

If you want the method to return the value you should:

  1. Make the method return type as double
  2. Use the return keyword

With return:

public class Main{

    static double price;

    public static void main(String[] args) {
        price = 10000;
        price = lowerPriceByPercentage(5);
        System.out.println(price);
    }

    public static double lowerPriceByPercentage(double percent){
        return price - price*(percent/100);
    }
}

Run this online: https://onlinegdb.com/bqx3vcYkA

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anuj Kumar
Solution 2