防止JSON.parse(data)截断String浮点数的零位数 [英] Prevent JSON.parse(data) from cutting off zero digit for String floats

查看:91
本文介绍了防止JSON.parse(data)截断String浮点数的零位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用图表绘制图表.我目前有一个要转换为如下所示的JSON数组的字符串:

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]}]}]

我需要尾随零,以便1020.90保持1020.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]}]}

1020.90转换为1020.9.我认为这是float的行为,但可以将其转换为1020.90 [出于显示目的]吗?我需要使用highcharts来显示数据.

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.

推荐答案

防止JSON.parse(data)截断字符串浮点数的零位数

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

这与JSON无关;这是关于数字在JavaScript中的工作方式

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

1020.90转换为1020.9

The 1020.90 converts to 1020.9

这里没有转换".他们是相同的数字.

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

使用highcharts显示数据时,我需要这个

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.

这篇关于防止JSON.parse(data)截断String浮点数的零位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆