js formatted amount, optional with or without thousandths, optional retention of precision, also found online, but no problem to use
/*
Round the value and format it.
@param num value (Number or String)
@param cent The decimal to be retained Bit (Number)
@param isThousand whether thousandth place is required 0: not required, 1: required (numeric type);
@return string in format, such as '1,234,567.45'
@type String
*/
function formatNumber(num,cent,isThousand){
num = num.toString().replace(/$|,/g,'');
if(isNaN(num)) //Check that the incoming value is a numeric type.
num = "0";
if(isNaN(cent))//Ensure that the incoming decimal place is a numeric value.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//Find the number of decimal places and make sure it is a positive integer.
if(isNaN(isThousand))//Make sure whether the input is required Thousands is a numeric type.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >= 1) //Make sure the value passed in is only 0 or 1
isThousand = 1;
sign = (num == (num = Math.abs(num))); //Get the sign (positive/negative number )
//Math.floor: Returns the largest integer less than or equal to its numerical parameter
num = Math.floor(num*Math.pow(10,cent) 0.50000000001);//Convert the specified decimal places first into an integer. Extra decimal places are rounded off.
cents = num%Math.pow(10,cent); //Find the decimal place value.
num = Math.floor(num/Math.pow(10, cent)).toString();//Find the integer digit value.
cents = cents.toString();//Convert the decimal digits into a string to find the length of the decimal digits.
while(cents. lengthcents = "0" cents;
}
if(isThousand == 0) //No thousandth place character required.
return (((sign)?'':'-') num '.' cents);
//Format the integer part into thousandths.
for (var i = 0; i < ; Math.floor((num.length-(1 i))/3); i )
num = num.substring(0,num.length-(4*i 3)) ','
num .substring(num.length-(4*i 3));
return (((sign)?'':'-') num '.' cents);
}