Copy codeThe Code is as follows:
/**
* Format a number
* Author: Z, Mingyu
* Parameters:
* PrmNum (Number): Number to be formatted
* PrmPtn (String): formatting rule, for example, #,## 0.00
** PrmNullValue: The result returned when the number to be formatted is null, null, or non-numeric. The default value is 0.
*/
Function formatNum (prmNum, prmPtn, prmNullValue ){
Var nullValue = prmNullValue? PrmNullValue: 0;
If (prmNum = "" | prmNum = null) return nullValue;
PrmNum = prmNum. replace (//,/g ,"");
If (isNaN (prmNum) return nullValue;
If (prmPtn = "" | prmPtn = null) return prmNum;
Var ptnLen = prmPtn. length;
Var pointIndex = prmPtn. indexOf (".");
Var groupIndex = prmPtn. indexOf (",");
Var pointLen = (pointIndex =-1? 0: ptnLen-pointIndex-1 );
PrmNum = parseFloat (prmNum). toFixed (pointLen) + "";
If (groupIndex! =-1 ){
If (pointIndex =-1) pointIndex = ptnLen;
Var len = pointIndex-groupIndex-1;
Var reg = new RegExp ("(-? \ D +) (\ d {"+ len + "})");
While (reg. test (prmNum) prmNum = prmNum. replace (reg, "$1, $2 ");
}
Return prmNum;
}