简体   繁体   English

如何将浮点值四舍五入为双值?

[英]How to round a float value to double value?

The follow code round incorrectly for a human people: 对于人类,以下代码错误地四舍五入:

double d = 0.7F;
System.out.println( d );

print the value: 0.699999988079071 打印值:0.699999988079071

The follow code work better: 以下代码可以更好地工作:

double d = Double.valueOf( Float.toString( 0.7F ));
System.out.println( d );

But create an additional String. 但是创建一个额外的字符串。 Are there a more performance conversion of float to double without an extra string? 在没有额外的字符串的情况下,float有更多的性能转换成double吗?

The code line are simple and demonstrate only the problem. 代码行很简单,仅演示了问题所在。 The program is more complex. 该程序更复杂。 The float values are in a one part of a complex model. 浮点值位于复杂模型的一部分中。 The formatting layer use only double. 格式化层仅使用double。 Also DecimalFormat accept only double and not float. 此外,DecimalFormat仅接受双精度而不是浮点型。

Yes, use 是的,使用

double d = 0.7D;

Using F means it's a float constant, and then it's promoted to double and may not be the closest number to 0.7 that can be represented with a double precision floating point, as the mantissa is not extended in any way. 使用F表示它是一个float常量,然后将其提升为double并且可能不是可以用双精度浮点表示的最接近0.7的数字,因为尾数没有任何方式扩展。

0.7D is a double constant to begin with. 0.7D是一个double常量。

if it's all about the output (for humans) i would suggest to use printf: 如果这一切都是关于输出(对于人类),我建议使用printf:

System.out.printf("Value: %.2f", d);

doing the conversion with FloatingDecimal is surely faster, and prints 0.7 as well 使用FloatingDecimal进行转换肯定会更快,并且还会打印0.7

  Double doubleResult = new FloatingDecimal(0.7F).doubleValue();

But create an additional String. 但是创建一个额外的字符串。 Are there a more performance conversion of float to double without an extra string? 在没有额外的字符串的情况下,float有更多的性能转换成double吗?

Allocating a short-lived block of a few bytes is only the tip of the iceberg. 分配一个短暂的几个字节的块只是冰山一角。 The “conversion” that you desire fundamentally requires going from binary floating-point to decimal representation and then back to binary floating-point, and these conversions aren't cheap regardless of how the intermediate decimal representation is stored. 从根本上讲,您希望进行的“转换”需要从二进制浮点数转换为十进制表示,然后再回到二进制浮点,并且这些转换并不便宜,无论中间十进制表示形式如何存储。

If you know that the float values you are concerned with come from decimal values with two digits after the dot, you could use: 如果您知道所关心的float值来自点后两位两位的十进制值,则可以使用:

double d = Math.round(f * 100.0) / 100.0;

This incurs only one division plus Math.round which is cheaper than one conversion to or from decimal. 这只会产生一个除法Math.round ,再加上Math.round ,这比一次转换为十进制或从十进制转换便宜。 And any decent compiler should produce code for it that does not allocate. 而且任何体面的编译器都应为其生成未分配的代码。

If you know what precision you want you can round it to say 6 decimal places. 如果知道所需的精度,可以四舍五入为小数点后六位。 eg. 例如。

public static double round6(double x) {
    return Math.round(x * 1e6) / 1e6;
}

round6(0.7f) == 0.7

The fact the x passed in was a float doesn't matter provided this precision is suitable. 只要此精度合适,传入的x就是float并不重要。

double d = 0.7F;
System.out.println( d );

print the value: 0.699999988079071 打印值:0.699999988079071

The conversion is correct. 转换正确。

When you print out a float f , it prints out enough digits so that the value it prints out is closer to the true value of f than any other float . 当您打印float f ,它会打印出足够的数字,因此它所打印的比其他任何float都更接近f真实值

When you print out a double , it prints out enough digits so that the value it prints out is closer to the true value of d then any other double . 当您打印出一个double ,它会打印出足够的数字,因此它打印出来的值比其他double值都更接近d的真实值。

The conversion from float to double is exact, it's just showing you a value closer to the true value you'd had in your float . 从float到double的转换是准确的,它只是向您显示一个更接近float真实值的值。 Double.valueOf( Float.toString( 0.7F )) is not more accurate; Double.valueOf( Float.toString( 0.7F ))不太准确; it just pretends your values are nicer than they actually are. 它只是假装您的价值比实际价值要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM