to:
3.50我已经使用了这两种方法:
DecimalFormat df = new DecimalFormat(0.00); double result = Double.valueOf(df.format(input)); System.out.println(answer);和
public double round(double input) { int decimalPlace = 2; BigDecimal bd = new BigDecimal(input); bd = bd.setScale(decimalPlace,BigDecimal.ROUND_UP); return(bd.doubleValue()); }但是它保持打印:
3.5有没有人有这个解决方案?因为我真的认为这应该是正常的。感谢您的帮助。
解决方案您的第一个解决方案真的很真实。只要这样做:
DecimalFormat df = new DecimalFormat(0.00); System.out.println(df.format(input));您的版本不起作用的原因是您正在使用输入 double,将其格式化为一个 String 与2dp,然后将其转换回 double 。 System.out.println 然后像往常一样打印 double ,没有特殊的小数位数规则。 / p>
I want to round this double:
3.499999999999999to:
3.50And I already used these two methods:
DecimalFormat df = new DecimalFormat("0.00"); double result = Double.valueOf(df.format(input)); System.out.println(answer);and
public double round(double input) { int decimalPlace = 2; BigDecimal bd = new BigDecimal(input); bd = bd.setScale(decimalPlace,BigDecimal.ROUND_UP); return (bd.doubleValue()); }But It keeps printing:
3.5Does anyone have a solution for this? Because I really think that this should work. Thanks for your help.
解决方案Your first solution is really, really close. Just do this:
DecimalFormat df = new DecimalFormat("0.00"); System.out.println(df.format(input));The reason your version doesn't work is that you're taking the input double, formatting it as a String with 2dp, but then converting it back to a double. System.out.println is then printing the double as it always would, with no special decimal-place rules.
更多推荐
格式化双精度到2位小数不起作用
发布评论