# JavaScript: numbers with decimal (thousand) separator

Today, I needed a JavaScript function that converts a number like 1234567 to something more beautiful like 1.234.567. And because I didn’t need to take notice of locale settings (like using a comma instead of a dot) most solutions on the web where just too much for me and I decided to write my own.

Mainly its a 3-liner, but packed in a function it becomes a little more. Feel free to use it for your own:

function thousand_sep(nr) {
var nr_tmp = nr.toString();
for (var dots = Math.ceil((nr_tmp.length / 3) - 1); dots > 0; dots--) {
nr_tmp = nr_tmp.substr(0, nr_tmp.length - (dots * 3)) + '.' +
nr_tmp.substring(nr_tmp.length - (dots * 3), nr_tmp.length);
}
return (nr_tmp);
}