I have created a Map in groovy:
def measureMap = [:]
measureMap.put('engine.temperature', 70.0)
measureMap.put('fuel.level', 71.0)
The values for this map considered as Bigdecimal.
Is there any reason for this.
Why I am asking this i know the hierarchy
java.lang.Object
java.lang.Number
java.math.BigDecimal
so I thought by default it should consider it as double/float .
By default, Groovy uses BigDecimal for decimal numbers. This is documented:
Conveniently for exact decimal number calculations, Groovy choses java.lang.BigDecimal as its decimal number type. In addition, both float and double are supported, but require an explicit type declaration, type coercion or suffix. Even if BigDecimal is the default for decimal numbers, such literals are accepted in methods or closures taking float or double as parameter types.
If you need your values to be double-typed in the map, you can add the familiar D or F suffixes for double/float literals:
measureMap.put('engine.temperature', 70.0d) //java.lang.Double
measureMap.put('engine.temperature', 71.0f) //java.lang.Float
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With