|
@@ -227,5 +227,41 @@ module.exports = {
|
|
|
},
|
|
|
getPinYinCamelChars: function(value) {
|
|
|
return pinyin.getCamelChars(value);
|
|
|
+ },
|
|
|
+ formatNumber: function(formatStr, val) {
|
|
|
+ let rst = val;
|
|
|
+ if (formatStr) {
|
|
|
+ if (!(isNaN(parseFloat(val)))) {
|
|
|
+ function comdify(numStr){
|
|
|
+ let re = /\d{1,3}(?=(\d{3})+$)/g;
|
|
|
+ return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
|
|
|
+ }
|
|
|
+ let dotIdx = formatStr.indexOf(".");
|
|
|
+ if (dotIdx >= 0) {
|
|
|
+ let tmpStr = parseFloat(val).toFixed(formatStr.length - dotIdx - 1);
|
|
|
+ let digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
|
|
|
+ for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
|
|
|
+ if (digStr[sIdx] === '#') {
|
|
|
+ if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
|
|
|
+ tmpStr = tmpStr.substr(0, tmpStr.length - 1);
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
|
|
|
+ rst = tmpStr;
|
|
|
+ } else {
|
|
|
+ rst = parseFloat(val).toFixed(0);
|
|
|
+ }
|
|
|
+ let commaIdx = formatStr.indexOf(",");
|
|
|
+ if (commaIdx >= 0) {
|
|
|
+ rst = comdify(val.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rst;
|
|
|
}
|
|
|
-};
|
|
|
+}
|