百分之一的折扣无效。如何将这个问题降低到最低?

huangapple go评论66阅读模式
英文:

One percent discount doesn't work. How do I round this issue down?

问题

问题: 你好,我创建了一个带有折扣的方法,但1%的折扣没有起作用。是数字四舍五入的问题吗?

等待: 价格65。

实际: 价格66。

代码:

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

请告诉我如何向下取整?

解决方案:

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);
    }
}
英文:

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);

    }
}

答案1

得分: 1

因为您正在使用 int 来存储价格等信息。尝试使用浮点数,如 floatdouble

英文:

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

huangapple
  • 本文由 发表于 2020年3月16日 18:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/60703865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定