Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy By default in Map Numeric values considered as Big decimal

Tags:

groovy

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 .

like image 963
GauravRatnawat Avatar asked Dec 03 '25 09:12

GauravRatnawat


1 Answers

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
like image 165
ernest_k Avatar answered Dec 05 '25 00:12

ernest_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!