js-数字操作与功能实现-运用
1 数字末尾加万
function formatWan(n) { n = n.toString(); if ( n < 10000) { return ( n / 10000 ).toFixed((5-n.length)) + '万'; } else { return ( n / 10000 ).toFixed(1) + '万'; } } console.log( formatWan(12345) ); // '1.2万' console.log( formatWan(123) ); // '0.01万' console.log( formatWan(1) ); // '0.0001万'
2 数字指定数位不足补零
Number.prototype.lenWithZero = function(oCount) { var strText = this.toString(); while(strText.length < oCount) { strText = `0${strText}`; }; return strText; } let num = 123; console.log(num.lenWithZero(5)); // '00123'