转换为 JSON 时的有效数字
Significant digits when converting to JSON
我有一个 mat
的矩阵 lat/lon 坐标,精确到小数点后第 12 位。我的 objective 将矩阵转换为 JSON 以保持这些数字的完整性。
> head(mat)
[,1] [,2]
[1,] -122.6790 45.51459
[2,] -122.6789 45.51458
[3,] -122.6789 45.51457
[4,] -122.6789 45.51457
[5,] -122.6788 45.51456
[6,] -122.6788 45.51455
> print(head(mat), digits = 15)
[,1] [,2]
[1,] -122.678966434126 45.5145902219176
[2,] -122.678937015334 45.5145823126032
[3,] -122.678907596543 45.5145744032888
[4,] -122.678878177752 45.5145664939744
[5,] -122.678848758960 45.5145585846600
[6,] -122.678819340169 45.5145506753456
然而,简单地使用 toJSON() 只能保存 4 位数字。
> toJSON(mat)
[[-122.679,45.5146],[-122.6789,45.5146],[-122.6789,45.5146],[-122.6789,45.5146]...
在 R 中转换为 JSON 时如何保留这些有效数字?
library(jsonlite)
toJSON(mat, digits = 12)
我有一个 mat
的矩阵 lat/lon 坐标,精确到小数点后第 12 位。我的 objective 将矩阵转换为 JSON 以保持这些数字的完整性。
> head(mat)
[,1] [,2]
[1,] -122.6790 45.51459
[2,] -122.6789 45.51458
[3,] -122.6789 45.51457
[4,] -122.6789 45.51457
[5,] -122.6788 45.51456
[6,] -122.6788 45.51455
> print(head(mat), digits = 15)
[,1] [,2]
[1,] -122.678966434126 45.5145902219176
[2,] -122.678937015334 45.5145823126032
[3,] -122.678907596543 45.5145744032888
[4,] -122.678878177752 45.5145664939744
[5,] -122.678848758960 45.5145585846600
[6,] -122.678819340169 45.5145506753456
然而,简单地使用 toJSON() 只能保存 4 位数字。
> toJSON(mat)
[[-122.679,45.5146],[-122.6789,45.5146],[-122.6789,45.5146],[-122.6789,45.5146]...
在 R 中转换为 JSON 时如何保留这些有效数字?
library(jsonlite)
toJSON(mat, digits = 12)