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

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 中看到如何将数字转换为字符串,只保留 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)