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

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

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

问题

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

等待: 价格65。

实际: 价格66。

代码:

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

请告诉我如何向下取整?

解决方案:

  1. public class Main {
  2. public static void main(String[] args) {
  3. int discount = 1;
  4. int price = 66;
  5. double amountOfDiscount = (discount * (price / 100.0f));
  6. double priceWithDiscountDoubleType = (price - amountOfDiscount);
  7. int priceWithDiscount = (int) Math.floor(priceWithDiscountDoubleType);
  8. System.out.println(priceWithDiscount);
  9. }
  10. }
英文:

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:

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

Please tell me how to round it down ?

SOLUTION:

  1. public class Main {
  2. public static void main(String[] args) {
  3. int discount = 1;
  4. int price = 66;
  5. double amountOfDiscount = (discount * (price / 100.0f));
  6. double priceWithDiscountDoubleType = (price - amountOfDiscount);
  7. int priceWithDiscount = (int)
  8. Math.floor(priceWithDiscountDoubleType);
  9. System.out.println(priceWithDiscount);
  10. }
  11. }

答案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:

确定