Java: How to set Precision for double value?

I was working with numbers recently and I had a situation where I want to set the precision of a double value say to 6 digits or 4 digits, depending on the value stored in the database.

For example, If in the database the precision is set as 4 digits then the output must look as,

10.0000.

I tried with DecimalFormat and using the string ##.####, but it is annoying to use symbols everytime.

Is there any better approach, say something like below:

Double value = 10.0;
value.setPrecision(4);
460891 次浏览

不能将 Double (或 Double)的精度设置为指定的小数位数,因为浮点值没有小数位。他们有二进制数字。

You will have to convert into a decimal radix, either via BigDecimal or DecimalFormat, depending on what you want to do with the value later.

请参阅我对 这个问题的回答,以反驳不可避免的 * 100/100答案。

The precision of double and float is fixed by their size and the way the 浮点类型 are implemented.

另一方面,输出中的小数位数与格式有关。您说得对,反复输入相同的常量是一个糟糕的主意。应该声明一个字符串常量,并使用其符号表示形式。

private static final String DBL_FMT = "##.####";

使用符号表示可以在使用常量的所有位置改变精度,而无需在代码中进行搜索。

To expand on @EJP, the concept of 'precision' when dealing with doubles is extremely fraught. As discussed in https://stackoverflow.com/a/3730040/390153 you can't even represent 0.1 as a double regardless of the precision, for the same reason you can't represent 1/3 in base 10 with finite precision.

你需要考虑你正试图解决的问题,并考虑:

A)首先我是否应该使用双精度; 如果精度是一个相关概念,那么使用双精度很可能是一个错误。

B)如果双打是合适的,我所说的精确度是什么意思?如果您只是在讨论显示,那么将逻辑封装在一个显示函数中,您只需要在一个地方处理它;。应用干燥原理。

public static String setPrecision(String number, int decimal) {
double nbr = Double.valueOf(number);
int integer_Part = (int) nbr;
double float_Part = nbr - integer_Part;
int floating_point = (int) (Math.pow(10, decimal) * float_Part);
String final_nbr = String.valueOf(integer_Part) + "." + String.valueOf(floating_point);
return final_nbr;
}

也许这种方法可以帮助您精确计算双精度值。

double truncate(double number)
{
int integerPart = (int) number;
double fractionalPart = number - integerPart;
fractionalPart *= 100;  //It is for upto two decimal values after point.
//You can increase the zeros to fulfill your needs.
int fractPart = (int) fractionalPart;
fractionalPart = (double) (integerPart) + (double) (fractPart)/100;
return fractionalPart;
}

此方法允许设置精度级别。

double truncate(double number, int precision)
{
double prec = Math.pow(10, precision);
int integerPart = (int) number;
double fractionalPart = number - integerPart;
fractionalPart *= prec;
int fractPart = (int) fractionalPart;
fractionalPart = (double) (integerPart) + (double) (fractPart)/prec;
return fractionalPart;
}

这是一个简单的方法:

String formato = String.format("%.2f");

It sets the precision to 2 digits.

如果只想打印,可以这样使用:

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

您可以为此目的尝试 BigDecimal

Double toBeTruncated = new Double("3.5789055");


Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
.setScale(3, RoundingMode.HALF_UP)
.doubleValue();
BigDecimal value = new BigDecimal(10.0000);
value.setScale(4);

这里有一个有效的方法来实现这个结果,但有两点需要注意。

  1. 将精度限制为“最大”N 个数字(不固定为 N 个数字)。
  2. 四舍五入的数字(不是四舍五入到最近的)。

在这里查看示例测试用例。

123.12345678 ==> 123.123
1.230000 ==> 1.23
1.1 ==> 1.1
1 ==> 1.0
0.000 ==> 0.0
0.00 ==> 0.0
0.4 ==> 0.4
0 ==> 0.0
1.4999 ==> 1.499
1.4995 ==> 1.499
1.4994 ==> 1.499

这是密码。我上面提到的两个注意事项可以很容易地解决,然而,速度对我来说比准确性更重要,所以我把它留在这里。 与数学操作相比,像 System.out.printf("%.2f",123.234);这样的字符串操作在计算上是昂贵的。在我的测试中,下面的代码(没有 sysout)所花的时间是 String 操作的1/30。

public double limitPrecision(String dblAsString, int maxDigitsAfterDecimal) {
int multiplier = (int) Math.pow(10, maxDigitsAfterDecimal);
double truncated = (double) ((long) ((Double.parseDouble(dblAsString)) * multiplier)) / multiplier;
System.out.println(dblAsString + " ==> " + truncated);
return truncated;
}

对双值 DecimalFormat进行精度设定是一项很好的技术。使用此类导入 java.text.DecimalFormat并创建它的对象,例如

double no=12.786;
DecimalFormat dec = new DecimalFormat("#0.00");
System.out.println(dec.format(no));

所以它会在小数点后面打印两位数字它会打印12.79

这对我很有效:

public static void main(String[] s) {
Double d = Math.PI;
d = Double.parseDouble(String.format("%.3f", d));  // can be required precision
System.out.println(d);
}