programming-examples/js/Math/Print an integer with commas as thousands separators.js

10 lines
391 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function thousands_separators(num)
{
var num_parts = num.toString().split(".");
num_parts[0] = num_parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return num_parts.join(".");
}
console.log(thousands_separators(1000));
console.log(thousands_separators(10000.23));
console.log(thousands_separators(100000));