netplex / json-smart-v2

Apache License 2.0
149 stars 68 forks source link

Maintain the precision of a decimal number. #195

Closed mateusneresrb closed 1 week ago

mateusneresrb commented 3 months ago

Hello, good afternoon, is there any way to maintain the precision of a number when serializing? When I try to serialize the number 10.50, it converts it to a double and removes the final 0, thus maintaining the precision.

I'm using json-smart together with jsonpath.

mateusneresrb commented 3 months ago

It ends up in the Double.parseDouble of this class and removes the 0. The correct thing to do would be to return a BigDecimal, but this scenario only occurs with a string longer than 18 characters.

https://github.com/netplex/json-smart-v2/blob/4b1a6b516b10deec9edbaabf8eaccefa14667628/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java#L152-L180

UrielCh commented 3 months ago

10.50 and 10.5 are the same value. fill free to re-format flot number to keep extra tailling zero, or store you digits as string.

mateusneresrb commented 3 months ago

10,50 e 10,5 são o mesmo valor. preencha livre para reformatar o número do flot para manter o zero de rejeitos extras ou armazene seus dígitos como string.

I see, they're the same value, but after I turn it into a bigdecimal, because I need the 0, it ends up removing it. One option would be to use a custom mapper?

UrielCh commented 1 week ago

Update your model and store your numbers as strings.