applecrusher
applecrusher

Reputation: 5648

Prevent JSON.parse(data) from cutting off zero digit for String floats

I am trying to use highcharts to make graphs. I currently have a string which I am looking to convert to a JSON array which looks like the following:

[{"chart":{"type":"line","renderTo":"chart_0"},"title":{"text":"Daily Sales & Spend"},
"xAxis":{"categories":["08-03-2015","08-04-2015"]},
"yAxis":{"title":{"text":"Dollars"}},
"series":[{"name":"Spend","data":[73.84,73.75]},{"name":"Sales","data":[1020.90,4007.90]}]}]

I need the trailing zeros so that 1020.90 stays 1020.90, on conversion the data becomes the following after the first index:

{"chart":{"type":"line","renderTo":"chart_0"},
"title":{"text":"Daily Sales & Spend"},
"xAxis":{"categories":["08-03-2015","08-04-2015"]},
"yAxis":{"title":{"text":"Dollars"}},
"series":[{"name":"Spend","data"[73.84,73.75]},{"name":"Sales","data":[1020.9,4009.9]}]}

The 1020.90 converts to 1020.9. I think this is a behavior of the float, but is it possible to convert it to 1020.90 [for display purposes]? I need this for displaying the data using highcharts.

Upvotes: 0

Views: 2410

Answers (1)

user229044
user229044

Reputation: 239311

Prevent JSON.parse(data) from cutting off zero digit for String floats

This has nothing to do with JSON; this is about how numbers work in JavaScript

The 1020.90 converts to 1020.9

There is no "conversion" here. They're the same number.

I need this for displaying the data using highcharts

Then you need to pad the number with trailing zeros when you convert it to a string.

It is impossible to tell a float how many significant digits it has. You can only impose significant digits when you display the float as a string.

Upvotes: 4

Related Questions