java round up and down

Take float f = 1.65fan example

Improvement arrangement

Math.ceil(f) //2.0

Round down

Math.floor(f) //1.0

rounding

Math.rint(f) //2.0

Insert picture description here
When float f = 2.5f, the following result is rounded and we hope there are some differences.

Math.rint(f) //2.0

Insert picture description here
Math.rint.5When it encounters an even result, the return value of the above three methods are all doubletypes.
Using the Math.roundresult of rounding the return value int, 2.5fthe return value 3.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/109997639
Recommended