开发者社区> 问答> 正文

百分之一的折扣不起作用。如何解决这个问题?

美好的一天,我给数字打折的方法,但是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);

    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 17:42:40 852 0
1 条回答
写回答
取消 提交回答
  • 这是因为您int用来存储价格等。请尝试使用浮点数,例如float或double

    回答来源:Stack Overflow

    2020-03-22 17:42:51
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载