I have a script that takes a big number and counts up. The script converts the number to a string so that it can be formatted with commas, but I also need to add a decimal place before the last two digits. I know that this line handles the commas:
if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
Is there a similar line of code I can add that accomplishes adding a decimal point?
Yes, if you always want the decimal before the last two:
function numberIt(str) {
//number before the decimal point
num = str.substring(0,str.length-3);
//number after the decimal point
dec = str.substring(str.length-2,str.length-1)
//connect both parts while comma-ing the first half
output = commaFunc(num) + "." + dec;
return output;
}
When commaFunc()
is the function you described that adds commas.
EDIT
After much hard work, the full correct code: