1
/*
*
2 * 格式化数字
3 * Author : Z,Mingyu
4 * 参数:
5 * prmNum (Number) : 要格式化的数字
6 * prmPtn (String) : 格式化规则,例如:#,##0.00
7 * * prmNullValue : 当要格式化的数字为null、空或非数字时,返回的结果。默认为0
8 */
9 function formatNum(prmNum,prmPtn,prmNullValue){
10 var nullValue = prmNullValue ? prmNullValue : 0 ;
11 if (prmNum == "" || prmNum == null ) return nullValue;
12 prmNum = prmNum.replace( / \, / g, "" );
13 if (isNaN(prmNum)) return nullValue;
14 if (prmPtn == "" || prmPtn == null ) return prmNum;
15 var ptnLen = prmPtn.length;
16 var pointIndex = prmPtn.indexOf( " . " );
17 var groupIndex = prmPtn.indexOf( " , " );
18 var pointLen = (pointIndex == - 1 ? 0 : ptnLen - pointIndex - 1 );
19 prmNum = parseFloat(prmNum).toFixed(pointLen) + "" ;
20 if (groupIndex != - 1 ){
21 if (pointIndex == - 1 ) pointIndex = ptnLen;
22 var len = pointIndex - groupIndex - 1 ;
23 var reg = new RegExp( " (-?\\d+)(\\d{ " + len + " }) " );
24 while (reg.test(prmNum)) prmNum = prmNum.replace(reg, " $1,$2 " );
25 }
26 return prmNum;
27 }
2 * 格式化数字
3 * Author : Z,Mingyu
4 * 参数:
5 * prmNum (Number) : 要格式化的数字
6 * prmPtn (String) : 格式化规则,例如:#,##0.00
7 * * prmNullValue : 当要格式化的数字为null、空或非数字时,返回的结果。默认为0
8 */
9 function formatNum(prmNum,prmPtn,prmNullValue){
10 var nullValue = prmNullValue ? prmNullValue : 0 ;
11 if (prmNum == "" || prmNum == null ) return nullValue;
12 prmNum = prmNum.replace( / \, / g, "" );
13 if (isNaN(prmNum)) return nullValue;
14 if (prmPtn == "" || prmPtn == null ) return prmNum;
15 var ptnLen = prmPtn.length;
16 var pointIndex = prmPtn.indexOf( " . " );
17 var groupIndex = prmPtn.indexOf( " , " );
18 var pointLen = (pointIndex == - 1 ? 0 : ptnLen - pointIndex - 1 );
19 prmNum = parseFloat(prmNum).toFixed(pointLen) + "" ;
20 if (groupIndex != - 1 ){
21 if (pointIndex == - 1 ) pointIndex = ptnLen;
22 var len = pointIndex - groupIndex - 1 ;
23 var reg = new RegExp( " (-?\\d+)(\\d{ " + len + " }) " );
24 while (reg.test(prmNum)) prmNum = prmNum.replace(reg, " $1,$2 " );
25 }
26 return prmNum;
27 }
宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒