function pick() { const args = arguments; const length = args.length; for (let i = 0; i < length; i++) { const arg = args[i]; if (typeof arg !== 'undefined' && arg !== null) { return arg; } } } var defaultoptions = { decimalpoint: '.', thousandssep: ',' }; //判断是不是一个数字 var isnumber = function (value) { // 将不是字符串转化成字符串 let val = typeof value === "string" ? value : string(value); let regexp = new regexp("^[0-9]+(\\.[0-9]+)?$", "g"); let flag = true; if (!val.match(regexp)) { flag = false; } return flag; }; (function (global, factory) { if (typeof module === "object" && typeof module.exports === "object") { module.exports = global.document ? factory(global, true) : function (w) { if (!w.document) { throw new error("number requires a window with a document"); } return factory(w); }; } else { factory(global); } // pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function (window, noglobal) { function numformat() { return this } numformat.prototype.format = function ( number, decimals, decimalpoint, thousandssep ) { number = +number || 0; decimals = +decimals; var ret, fractiondigits; const origdec = (number.tostring().split('.')[1] || '').split('e')[0].length, exponent = number.tostring().split('e'), firstdecimals = decimals; if (decimals === -1) { // preserve decimals. not huge numbers (#3793). decimals = math.min(origdec, 20); } else if (!isnumber(decimals)) { decimals = 2; } else if (decimals && exponent[1] && exponent[1] < 0) { // expose decimals from exponential notation (#7042) fractiondigits = decimals + +exponent[1]; if (fractiondigits >= 0) { // remove too small part of the number while keeping the notation exponent[0] = (+exponent[0]).toexponential(fractiondigits) .split('e')[0]; decimals = fractiondigits; } else { // fractiondigits < 0 exponent[0] = exponent[0].split('.')[0] || 0; if (decimals < 20) { // use number instead of exponential notation (#7405) number = (exponent[0] * math.pow(10, exponent[1])) .tofixed(decimals); } else { // or zero number = 0; } exponent[1] = 0; } } // add another decimal to avoid rounding errors of float numbers. (#4573) // then use tofixed to handle rounding. const roundednumber = ( math.abs(exponent[1] ? exponent[0] : number) + math.pow(10, -math.max(decimals, origdec) - 1) ).tofixed(decimals); // a string containing the positive integer component of the number const strinteger = string(parseint(roundednumber)); // leftover after grouping into thousands. can be 0, 1 or 2. const thousands = strinteger.length > 3 ? strinteger.length % 3 : 0; // decimalpoint = pick(decimalpoint, defaultoptions.decimalpoint); thousandssep = pick(thousandssep, defaultoptions.thousandssep); // start building the return ret = number < 0 ? '-' : ''; // add the leftover after grouping into thousands. for example, in the // number 42 000 000, this line adds 42. ret += thousands ? strinteger.substr(0, thousands) + thousandssep : ''; if (+exponent[1] < 0 && !firstdecimals) { ret = '0'; } else { // add the remaining thousands groups, joined by the thousands separator ret += strinteger .substr(thousands) .replace(/(\d{3})(?=\d)/g, '$1' + thousandssep); } // add the decimal point and the decimal component if (decimals) { // get the decimal component ret += decimalpoint + roundednumber.slice(-decimals); } if (exponent[1] && +ret !== 0) { ret += 'e' + exponent[1]; } return ret; } numformat.prototype.numberformatwithsymbol = function (value, options) { var numericsymbols = options.numericsymbols || ["万", "亿", "万亿"]; var numsymmagnitude = options.numsymmagnitude || 10000;//量值 var symbol = options.symbol || "";//单位 var decimals = options.decimals || decimals;//精确位数 var isreal = options.isreal || isreal;//是否显示真实数字 var i = numericsymbols && numericsymbols.length, multi, ret, numericsymboldetector = math.abs(value) if (numericsymboldetector >= 1000) { while (i-- && typeof ret === 'undefined') { multi = math.pow(numsymmagnitude, i + 1) if ( // only accept a numeric symbol when the distance is more // than a full unit. so for example if the symbol is k, we // don't accept numbers like 0.5k. numericsymboldetector >= multi && // accept one decimal before the symbol. accepts 0.5k but // not 0.25k. how does this work with the previous? (value * 10) % multi >= 0 && numericsymbols[i] !== null && value !== 0 ) { // #5480 ret = this.format( value / multi, isreal ? decimals : 2 ) + numericsymbols[i]; } } } if (typeof ret === 'undefined') { if (math.abs(value) >= 10000) { // add thousands separators ret = this.format(value, decimals); } else { // small numbers ret = this.format(value, decimals); // #2466 } } return ret + symbol; } window.numberformat = new numformat(); return numberformat }));