How do I round a number in Groovy?
You can use:
Math.round(x * 100) / 100
If x
is a BigDecimal
(the default in Groovy), this will be exact.
If you're dealing with doubles or floats
You can simply use
assert xyz == 1.789
xyz.round(1) == 1.8
xyz.round(2) == 1.79