Java Double 总是舍入到小数点后两位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25981349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:37:32  来源:igfitidea点击:

Java Double Round off to 2 decimal always

javadecimalrounding

提问by Reddy

I am trying to round off the double values to 2 decimal digits, however it's not working in all scenarios

我试图将双精度值四舍五入为 2 个十进制数字,但是它并不适用于所有情况

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

public static void main(String[] args) {
    System.out.println(round(25.0,2));  //25.0 - expected 25.00
    System.out.println(round(25.00d,2)); //25.0 - expected 25.00
    System.out.println(round(25,2));   //25.0 - expected 25.00
    System.out.println(round(25.666,2));  //25.67
}

In short, no matter whether decimal exists or not, always hold the values upto 2 decimal even if it needs to pad additional zeros.

简而言之,无论十进制是否存在,即使需要填充额外的零,也始终将值保持为小数点后 2 位。

Any help is appreciated!

任何帮助表示赞赏!

采纳答案by Aivean

There are two things that can be improved in your code.

在您的代码中有两件事可以改进。

First, casting double to BigDecimal in order to round it is very inefficient approach. You should use Math.round instead:

首先,将 double 转换为 BigDecimal 以对其进行舍入是非常低效的方法。您应该改用 Math.round:

    double value = 1.125879D;
    double valueRounded = Math.round(value * 100D) / 100D;

Second, when you print or convert real number to string, you may consider using System.out.printf or String.format. In your case using format "%.2f" does the trick.

其次,当您打印或将实数转换为字符串时,您可以考虑使用 System.out.printf 或 String.format。在您的情况下,使用格式 "%.2f" 可以解决问题。

    System.out.printf("%.2f", valueRounded);

回答by RP-

You are converting BigDecimalback to doublewhich essentially trim the trailing zeros.

您正在转换BigDecimaldouble,基本上修剪尾随零。

You can return either BigDecimalor BigDecimal.toPlainString().

您可以返回BigDecimalBigDecimal.toPlainString()

public static String round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.toPlainString();
}

回答by DucRP

I use the format() function of String class. Much simpler code. The 2 in "%.2f" indicates the number of digits after the decimal point you want to display. The f in "%.2f" indicates that you are printing a floating point number. Here is the documentation on formatting a string (http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax)

我使用 String 类的 format() 函数。更简单的代码。“%.2f”中的2表示要显示的小数点后的位数。"%.2f" 中的 f 表示您正在打印一个浮点数。这是有关格式化字符串的文档(http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

double number = 12.34567;
System.out.println(String.format("%.2f", number));

回答by Manjunath Maruthi

This will work for you:

这对你有用:

public static void main(String[] args) {

    DecimalFormat two = new DecimalFormat("0.00"); //Make new decimal format

    System.out.println(two.format(25.0)); 

    System.out.println(two.format(25.00d));

    System.out.println(two.format(25));

    System.out.println(two.format(25.666));

}