在 Leaflet JavaScript 的弹出窗口中向数字添加逗号
Add commas to a number in a popup in Leaflet JavaScript
我创建了一个弹出窗口,里面有一个数字。正在从 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>'
}
})
使用 Intl.NumberFormat
constructor to build a formatter that has certain requirements. Like the type of currency you're trying to use. This is actually the working part of 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);
我创建了一个弹出窗口,里面有一个数字。正在从 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>'
}
})
使用 Intl.NumberFormat
constructor to build a formatter that has certain requirements. Like the type of currency you're trying to use. This is actually the working part of 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);