AsithaL
AsithaL

Reputation: 59

Decimal formatting issue in net.sf.json Object

We are using compile net.sf.json-lib:json-lib:2.4:jdk15

String requestBody =     {"resconfirmsvid":28,"reservationno":"H0028W090718","component":"Hotel","partnerid":2,"total":169002.15,"internalnote":"","currencycode":"USD","history":[{"id":"0","payment_type":"Payment","payment_method":"Cash","date":"2019-10-30","refnumber":"012525TRTY","amount":160000,"currency":"USD","component":"Hotel","resconfirmsvid":28}]}

requestBody = URLDecoder.decode(requestBody, "UTF-8");
JSONObject request = JSONObject.fromObject(requestBody);

Then request object as follows,

request = {"resconfirmsvid":28,"reservationno":"H0028W090718","component":"Hotel","partnerid":2,"total":169002.16,"internalnote":"","currencycode":"USD","history":[{"id":"0","payment_type":"Payment","payment_method":"Cash","date":"2019-10-30","refnumber":"012525TRTY","amount":160000,"currency":"USD","component":"Hotel","resconfirmsvid":28}]}

After convert json string into json object using fromObject "total" value changed to 169002.16 from 169002.15

Upvotes: 1

Views: 482

Answers (2)

hazzy1986
hazzy1986

Reputation: 103

I got a same issue, when i used net.sf.json-lib for coverting json string into Json object.once i used the below gradle version
compile 'net.sf.json-lib:json-lib:2.3:jdk15' issue was fixed sucessfully.

Upvotes: 3

Babyburger
Babyburger

Reputation: 1828

JSON does not specify a precision for decimal numbers. That's why it is recommended to use strings for decimal numbers instead.

Source: Why would you use a string in JSON to represent a decimal number - dthorpe

Upvotes: 1

Related Questions