[TIMOB-26974] JSON.Parse() is converting int to string when number is .9
| GitHub Issue | n/a | 
|---|---|
| Type | Bug | 
| Priority | Critical | 
| Status | Closed | 
| Resolution | Invalid | 
| Resolution Date | 2019-06-07T22:28:23.000+0000 | 
| Affected Version/s | n/a | 
| Fix Version/s | n/a | 
| Components | n/a | 
| Labels | n/a | 
| Reporter | Srdjan Lukic-Bardak | 
| Assignee | Gary Mathews | 
| Created | 2019-03-09T21:42:48.000+0000 | 
| Updated | 2019-06-07T22:28:23.000+0000 | 
Description
	I'm trying to debug the reson why JSON.Parse() is adding 0000001 at the end of some int numbers.
My raw output form the API looks like
RawInput
{"Weight":10.9,"BodyFatPercentage":20.5}
{"Weight":10.99,"BodyFatPercentage":50.5}
But when I use JSON.parse(this.responseText); I end up with JSON object that looks like this. And as you can see number 70,9 - 80,9 and 90,9 get somehow converted. And only numbers in 70,80 and 90
Parsed object
{
BodyFatPercentage = 20.5;
Weight="10.90000000000001";
}
{
BodyFatPercentage = 80.5;
Weight="10.98999999999999";
}
As you can see the parsed JSON objects property "Weight" is now a string, where BodyFatPercentage is still int. And if the number is .99 we get .989999
You can try it by doing this : var obj = JSON.parse('{ "Id": 52, "ElementId": "5e9e81ca-3980-4bf9-b7aa-bd3baa2d466c", "Chest": 60.9, "Waist": 70.9, "Hips": 80.9, "Thigh": 90.99, "Calf": 100.9 }') Ti.API.log(obj);
This is expected, you are running into a precision error. None of the numbers are integers, integers are whole numbers. The numbers are doubles and there's not enough accuracy to store them correctly.