英文:
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
来存储价格等信息。尝试使用浮点数,如 float
或 double
。
英文:
It is because you're using int
to store the prices etc. Try using floating point numbers like float
or double
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论