PHP Developer
PHP Developer

Reputation: 31

Stop JSON.parse() from removing trailing zeros from a json string data

I have created a JSON string as below :

<script>
var string = JSON.parse('{"items":[{"data":[5.1]}, {"values":[5.10]}, {"offer":[3.100]}, {"grandtotal":[121.9700]}]}');

$.each(string['items'][1]['values'], function(index, value) {
        console.log(value);
        var newval = value.toString();
        var abc = newval.split('.');
        console.log(abc[1]); //retuns 1 instead of 10
});
</script>

Upvotes: 2

Views: 2836

Answers (1)

Kvam
Kvam

Reputation: 2218

You need to use strings. Internally, in more or less any programming language, 5.1 and 5.100 have the same representation, whereas "5.1" and "5.100" will differ.

The JSON-data will then look like this:

var string = JSON.parse('{"items":[{"data":["5.1"]}, {"values":["5.10"]}, {"offer":["3.100"]}, {"grandtotal":["121.9700"]}]}');

Upvotes: 3

Related Questions