如何在 Java 中四舍五入到下一个百位

How to round up to the next hundred in Java

我想让数字商店类似于电子商务,然后我想在 Java (Android Studio) 中将整数四舍五入到下一个最高的 100 倍数,例如:

782->800

9876->9900

7987->8000

24523->24600

123412->123500 等....

为此,我有一些代码来处理这个问题,但有时我会遇到一些错误,这是我在 java

上的代码
    private int fee = 200;
private int getRoundedPrice(int i){
    int amount = i + fee;
    int roundedAmount = 0;
    if(amount < 10000){
        roundedAmount = Integer.parseInt(String.valueOf(amount).charAt(0) + "000");
    }else if(amount < 100000){
        roundedAmount = Integer.parseInt(String.valueOf(amount).substring(0,2) + "000");
    }else if(amount < 1000000){
        roundedAmount = Integer.parseInt(String.valueOf(amount).substring(0,3) + "000");
    }else if(amount < 10000000){
        roundedAmount = Integer.parseInt(String.valueOf(amount).substring(0,4) + "000");
    }
    return roundedAmount + rounding(amount-roundedAmount);
}

private int rounding(int amount){
    if(amount < 100){
        return 100;
    }else if(amount < 200){
        return 200;
    }else if(amount < 300){
        return 300;
    }else if(amount < 400){
        return 400;
    }else if(amount < 500){
        return 500;
    }else if(amount < 600){
        return 600;
    }else if(amount < 700){
        return 700;
    }else if(amount < 800){
        return 800;
    }else if(amount < 900){
        return 900;
    }else if(amount < 1000){
        return 1000;
    }
}

使用这段代码,有时可以,但有时会产生错误,例如:

123141->12400(我体验过)

我不明白问题出在哪里,但我认为这段代码不好(我的意思是不稳定),也许其他人可以帮助我为这个问题提供更好的代码解决方案...谢谢:)

可能

int processed = (int)(Math.ceil(input/100.0)*100);