Unexpected BigDecimal truncation while using JsonObject.fromJson(jsonString)

Hi,

While trying to persist a Couchbase record, the attributes of BigDecimal type are automatically rounded off to 15 decimal digits while using JsonObject.fromJson(…). I believe it uses float64 (double) implicitly.

SampleCode:
(ObjectMapper is used to form the below JsonString from a POJO containing two BigDecimal types - There is no problem in this conversion.)
JsonObject.fromJson("{“latitude”:25.2527777777777777777777777777,“longitude”:55.3644444444444}")

Expected Result:
{“latitude”:25.2527777777777777777777777777,“longitude”:55.3644444444444}

Actual Result:
{“latitude”:25.252777777777776,“longitude”:55.3644444444444}

Please suggest a solution to retain the digits.

N1QL uses float64 (golang native data type for decimal is float64) for JSON number. You can use ROUND() to round the number. If you need precision more than 15 digits, Convert the value to string and back in the application or USE SDK retrieve data directly.

1 Like