![](/img/trans.png)
[英]How can I round a float such that there are only two trailing digits after the decimal point?
[英]how can I reduce the digits after the decimal point to only 5 digit latitude longitude in string?
我有这个用terraformer-wkt使用leaflet生成的字符串:
POLYGON((-66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743))
我想将小数位数的上限减少到5位。
POLYGON((-66.85271 10.48805,-66.85351 10.48617,-66.85342 10.48525,-66.84864))
我在javascripts中看到了reduce如何将数字转换成字符串,仅保留5个小数,但我不知道该如何与我的字符串一起使用:
var num = -66.85271859169006;
var n = num.toFixed(5);
//result would be -66.85271
您可以使用正则表达式搜索字符串中的所有数字并将其替换:
var str = 'POLYGON((-66.85271859169006 10.488056634656399,-66.85351252555847 10.486178802289459,-66.85342669487 10.485250431517958,-66.84864163398743))'; console.log(str.replace(/\\d+\\.\\d+/g, function(match) { return Number(match).toFixed(5); }));
看到:
相关: 舍入至小数点后两位(仅在必要时)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.