问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/32080393/prevent-json-parsedata-from-cutting-off-zero-digit-for-string-floats