-1

Good day, I make a method with a discount on the number but the one percent discount does not work . Problem with rounding numbers ?

Waiting: price 65.

Reality: price 66.

CODE:

public class Main {
    public static void main(String[] args) {
        int discount = 1;
        int price = 66;
        price -= (int) (discount * (price / 100));
        System.out.println(price);
    }
}

Please tell me how to round it down ?

SOLUTION:

public class Main {
    public static void main(String[] args) {
        int discount = 1;
        int price = 66;


        double amountOfDiscount = (discount * (price / 100.0f));
        double priceWithDiscountDoubleType = (price - amountOfDiscount);
        int priceWithDiscount = (int) 
        Math.floor(priceWithDiscountDoubleType);

        System.out.println(priceWithDiscount);

    }
}
Gen Ts
  • 404
  • 2
  • 18

1 Answers1

1

It is because you're using int to store the prices etc. Try using floating point numbers like float or double

Fred
  • 220
  • 2
  • 8
  • 1
    Float/double for currency is as bad as integer. After a few operations you would end up with some cents missing. One should use BigInteger/BigDecimal for that. – Amongalen Mar 16 '20 at 10:17