I am using the below function to generate formatted comma separated currency value in javascript but its not working for certain scenarios:
1234 => 1,234 (correct)
1.03 => 1.3 (wrong)
how can i fix the issue in my below function:
function formatThousands(n, dp) {
var s = '' + (Math.floor(n)), d = n % 1, i = s.length, r = '';
while ((i -= 3) > 0) {
r = ',' + s.substr(i, 3) + r;
}
return s.substr(0, i + 3) + r + (d ? '.' + Math.round(d * Math.pow(10, dp || 2)) : '');
}
Thanks in advance