Comparing version 0.1.1 to 0.1.2
@@ -560,31 +560,40 @@ const _ = require("lodash"); | ||
} | ||
/** | ||
* 将数字转换为百分比的字符串 | ||
* - 精确到0.01% | ||
* @param {number} paramValue 要格式化的值 | ||
* @param {string} paramDefault 无效值后,返回的字符串 | ||
* @return {string} 格式化的百分比字符串 | ||
* - 对于paramValue为null或undefined | ||
* 百分之一,最低精度支持到0.0001% 超过的部分四舍五入 | ||
* @param {number | string} paramValue | ||
* @return {number} round后的值 | ||
*/ | ||
static formatPercentage(paramValue, paramDefault = '0.00%') { | ||
static roundPercentage(paramValue) { | ||
const minNumber = 1000000; | ||
const PercentNumber = 100; | ||
if (paramValue === null || paramValue === undefined) { | ||
return paramDefault; | ||
return 0; | ||
} | ||
let v = 0; | ||
if (typeof paramValue === 'string') { | ||
v = Math.round(Number.parseFloat(paramValue) * 10000); | ||
} else if(this.isNumber(paramValue)) { | ||
v = paramValue; | ||
} | ||
// 不是指定类型,视为0 | ||
let n = ''; | ||
let v = Math.round(Number.parseFloat(paramValue) * 10000); | ||
if (Number.isNaN(v)) { | ||
return paramDefault; | ||
return 0; | ||
} | ||
if (v < 0) { | ||
// 如果是小于0的数 | ||
n = '-'; | ||
v = Math.abs(v); | ||
} | ||
let lowNumber = (v % 100).toString().padStart(2, '0'); | ||
let highNumber = ((v - lowNumber) / 100).toString().padStart(1, '0'); | ||
return [n, highNumber, '.', lowNumber, '%'].join(''); | ||
v *= minNumber; | ||
return Math.round(v) / (minNumber / PercentNumber); | ||
} | ||
/** | ||
* 将数字转换为百分比的字符串 | ||
* - 精确到0.01% | ||
* @param {number | string} paramValue 要格式化的值 | ||
* @param {string} paramDefault 无效值后,返回的字符串 | ||
* @return {string} 格式化的百分比字符串 | ||
* - 对于paramValue为null或undefined | ||
*/ | ||
static formatPercentage(paramValue) { | ||
return this.roundPercentage(paramValue).toString() + '%'; | ||
} | ||
@@ -591,0 +600,0 @@ /** |
{ | ||
"name": "xmcommon", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "javascript common lib for es6", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,8 @@ # xmcommon | ||
## 0.1.2 | ||
- 2019-07-20 | ||
- \* 优化utils.formatPercentage生成成 | ||
- \+ 增加utils.roundPercentage,将百分比的值精度控制到 0.0001% | ||
## 0.1.1 | ||
@@ -36,0 +42,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136794
3966
217