在 Leaflet JavaScript 的弹出窗口中为数字添加逗号

时间:2021-03-22 20:58:36

标签: javascript leaflet geojson

我创建了一个包含数字的弹出窗口。正在从 geoJSON 文件导入数据。有没有办法包含逗号分隔符?我试过“.toLocaleString()”和“.toString()”。对我的语法的帮助将不胜感激,谢谢。

layers.one = L.geoJson(data, {
            filter: function (feature, layer) {
                return (feature.properties.list_price <= 250000);
            },
            // Creates popup
            onEachFeature: function (feature, layer) {
                // console.log(layer)  // To test for function operation
                layer.bindPopup(
                    '<h3>' +
                    feature.properties.full_address +
                    '</h3><hr><p>' +
                    '$' +
                    feature.properties.list_price.toLocaleString().replace(/B(?=(d{3})+(?!d))/g, ",")
                ) + '</p>'
            }
        })

enter image description here

1 个答案:

答案 0 :(得分:1)

使用 Intl.NumberFormat 构造函数构建具有特定要求的格式化程序。就像您尝试使用的货币类型一样。这实际上是 Number.prototype.toLocaleString() 的工作部分。

这也不需要您在字符串前加上 $ 符号,因为格式化程序会处理货币的表示方式。

const value = 289900;

const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency', 
  currency: 'USD',
});

const formattedValue = formatter.format(value);
console.log(formattedValue);