mbayloon mbayloon - 2 months ago 7
Groovy Question

How do I round a number in Groovy?

How do I round a number in Groovy? I would like to keep 2 decimal places.

For example (pseudo-code):

round(1.2334695) = 1.23
round(1.2686589) = 1.27

Answer

You can use:

Math.round(x * 100) / 100

If x is a BigDecimal (the default in Groovy), this will be exact.