gpt4 book ai didi

javascript - 如何将字符串中的小数点后的数字减少到只有 5 位纬度经度?

转载 作者:行者123 更新时间:2023-12-03 09:56:47 26 4
gpt4 key购买 nike

我使用传单通过terraformer-wkt生成了这个字符串:

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))

我在javascript中看到reduce如何将数字转换为字符串,仅保留5小数,但我不知道如何将其与我的字符串一起使用:

var num = -66.85271859169006; 
var n = num.toFixed(5);
//result would be -66.85271

最佳答案

您可以使用 regular expression搜索字符串中的所有数字并替换它们:

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);
}));

参见:

相关:Round to at most 2 decimal places (only if necessary)

关于javascript - 如何将字符串中的小数点后的数字减少到只有 5 位纬度经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com