Android double serialization issue

46 Views Asked by At

I have an API who return this data

 {
  "QUANTITYKG": 19.044,
  "QUANTITYPC": 6.0,
  "QUANTITYM": 36.0,
  "QUANTITYM2": 0.0,
 }

In my android project I have a data class to handle the API Response.

@Serializable
data class Data(
    @SerialName("QUANTITYKG")
    val quantityKg: Double,
    @SerialName("QUANTITYPC")
    val quantityPc: Double,
    @SerialName("QUANTITYM")
    val quantityM: Double,
    @SerialName("QUANTITYM2")
    val quantityM2: Double

) {

When I received the response on my Application instead of 19.044 for the QUANTITYKG I received 19.04.

I try many solutions like format the response, but nothing is working. Can you help me to find the issue please. Thank you !!

1

There are 1 best solutions below

0
Gabe Sechan On

This will never be 19.04, because 19.04 is not a valid floating point number. Valid floating point numbers are sums of powers of 2- 1/2, 1/4, 1/8, etc. .04 is not exactly represented in this. So the problem is in whatever code you were using to display it, not in the conversion itself.