我有数字(十进制),我想将其四舍五入为“最近的50”:
122 =>舍入到150
177 =>舍入到200
157 =>舍入到200
57 =>舍入到100;
37 =>舍入到50;
1557 =>舍入到1600
3537 =>舍入到3550
如何用java / groovy做到这一点?
最佳答案
Groovy x + (50 - (x % 50 ?: 50))
:
def round50up = { int x ->
x + ( 50 - ( x % 50 ?: 50 ) )
}
assert round50up( 122 ) == 150
assert round50up( 177 ) == 200
assert round50up( 157 ) == 200
assert round50up( 57 ) == 100
assert round50up( 37 ) == 50
assert round50up( 1557 ) == 1600
assert round50up( 3537 ) == 3550
assert round50up( 100 ) == 100
assert round50up( 200 ) == 200
assert round50up( 250 ) == 250
测试。