number-display
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -64,2 +64,10 @@ ## 1.0.0 | ||
- Add feature: roundingType. Now you can set the way to round the decimal in 'round', 'floor' or 'ceil', witch default to 'round'. | ||
- When the length is too small, number-display will return the origin value as a string, instead of throwing an error. | ||
- When the length is too small, number-display will return the origin value as a string, instead of throwing an error. | ||
## 2.1.1 | ||
**2019-12-05** | ||
- Change param decimal name to 'precision', and default to equal to param length, witch means no additional limit. | ||
- Opt the rounding function. | ||
- Change param comma name to 'separator'. |
28
index.js
@@ -0,7 +1,14 @@ | ||
const maxDoubleLength = 16; | ||
const rounding = (intStr, decimalStr, decimalLength, type) => { | ||
if (decimalStr.length <= decimalLength) { | ||
return [intStr || '', decimalStr || '']; | ||
} | ||
if (intStr.length + decimalLength > maxDoubleLength) { | ||
return [intStr || '', (decimalStr || '').slice(0, decimalLength)]; | ||
} | ||
decimalLength = Math.max(decimalLength, 0); | ||
const handler = type === 'ceil' ? Math.ceil : (type === 'floor' ? Math.floor : Math.round); | ||
const value = parseFloat(`${intStr}.${decimalStr}`); | ||
const rstValue = handler(+value + 'e' + decimalLength) / Math.pow(10, decimalLength); | ||
const rstStrs = String(rstValue).split('.') | ||
const rstStrs = | ||
String(handler(`${intStr}.${decimalStr}e${decimalLength}`) / Math.pow(10, decimalLength)).split('.'); | ||
return [rstStrs[0] || '', rstStrs[1] || '']; | ||
@@ -14,6 +21,6 @@ } | ||
* @param {number} [length] max display char length (better >= 5 to allow any number); default 9 | ||
* @param {number} [decimal] max decimal precision; default 2 | ||
* @param {number} [precision] max decimal precision; default equals length | ||
* @param {string} [placeholder] result when neither number nor text; default '' | ||
* @param {boolean} [allowText] allow text as results, if false text will convert to placeholder, text will be slice within length param; default true | ||
* @param {boolean} [comma] show commas between digits in group of 3, if there are rooms; default true | ||
* @param {boolean} [separator] show commas between digits in group of 3, if there are rooms; default true | ||
* @param {string} [roundingType] Rounding type of decimals, enum in 'round', 'floor' or 'ceil'; default 'round' | ||
@@ -23,8 +30,11 @@ */ | ||
length = 9, | ||
decimal = 2, | ||
precision, | ||
placeholder = '', | ||
allowText = true, | ||
comma = true, | ||
separator = true, | ||
roundingType = 'round', | ||
} = {}) => value => { | ||
if (precision == null) { | ||
precision = length; | ||
} | ||
placeholder = placeholder.slice(0, length); | ||
@@ -48,7 +58,7 @@ const type = typeof value; | ||
const negative = cells[1]; | ||
let [int, deci] = rounding(cells[2] || '0', cells[4] || '', decimal, roundingType); | ||
let [int, deci] = rounding(cells[2] || '0', cells[4] || '', precision, roundingType); | ||
const localeInt = int.replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
let currentLen = negative.length + localeInt.length + 1 + deci.length; | ||
if (comma && currentLen <= length) { | ||
if (separator && currentLen <= length) { | ||
deci = deci.replace(/0+$/, ''); | ||
@@ -55,0 +65,0 @@ return `${negative}${localeInt}${deci && '.'}${deci}`; |
{ | ||
"name": "number-display", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Display number smartly within a certain length.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -79,3 +79,3 @@ # number-display | ||
allowText: false, | ||
comma: false, | ||
separator: false, | ||
placeholder: '--' | ||
@@ -97,7 +97,7 @@ }); | ||
**decimal** | ||
**precision** | ||
( default: 2 ) | ||
( default: equals to 'length' ) | ||
The max decimal length. Note that this is only a constraint. The final precision will be calculated by length, and less than this param. There will be no decimal trailing zeros. | ||
The max decimal length. Note that this is only a constraint. The final precision will be calculated by length, and less than this param. This param is the same as 'length' by default, witch means no additional limit. There will be no decimal trailing zeros. | ||
@@ -116,3 +116,3 @@ **placeholder** | ||
**comma** | ||
**separator** | ||
@@ -119,0 +119,0 @@ ( default: true ) |
9814
79