开发者

Rounding when converting from double to float in java

I am trying to convert a Minimum Bounding Rectangle from double values to float values. After the conversion, I need the (float) rectangle to be equal to o开发者_JAVA技巧r contain the (double) rectangle (the float rectangle needs to be bigger than or equal to the double rectangle). To do that I want to be able to specify which way to round the double to convert it to float. So, when casting the "Top" of the rectangle, I would round up, but when casting the "Bottom" of the rectangle, I would round down.

Is there a class that allows me to do this?

Thanks.


Math.ceil( double ); - round up
Math.round( double ); - round down


You can compare the float to the double and if it needs to be slightly larger or smaller you can call floatToIntBits and intToFloatBits with an increment or decrement as required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜