I'm little bit confused using double value.
When I used as below :-
double foo = 20.46455
assert 20 == foo.round()
assert 20.46 == foo.round(2)
It's working fine. but when I used something like as :-
def foo = 20.46455
assert 20 == foo.round()
it throws :-
java.lang.NullPointerException
and
def foo = 20.46455
assert 20.46 == foo.round(2)
it throws :-
groovy.lang.MissingMethodException: No signature of method: java.math.BigDecimal.round() is applicable for argument types: (java.lang.Integer) values: [2] Possible solutions: round(java.math.MathContext), find(), pow(int), power(java.lang.Integer), find(groovy.lang.Closure), and(java.lang.Number)
It means by default in groovy, value preserve in BigDecimal and BigDecimal.round() expect java.math.MathContext as input.
But My confusion start when I'm using Math.round() which except double as input then why below statement is getting passed while groovy preserve by default in BigDecimal?
def foo = 20.46455
assert 20 == Math.round(foo)
And why I have to use .toDouble() to pass my test case while foo has value in double format as below?
def foo = 20.46455
assert 20 == foo.toDouble().round()
assert 20.46 == foo.toDouble().round(2)
Note :- I don't want to know how to round a double value, I just want to know why groovy behaves differently in each case??