number-display
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -72,2 +72,9 @@ ## 1.0.0 | ||
- Opt the rounding function. | ||
- Change param comma name to 'separator'. | ||
- Change param comma name to 'separator'. | ||
## 2.1.2 | ||
**2019-12-07** | ||
- Change back param precision name to 'decimal', to avoid confusion with the common 'toPrecision' meaning. | ||
- Add inner precision limit to avoid float error. |
22
index.js
@@ -1,2 +0,2 @@ | ||
const maxDoubleLength = 16; | ||
const maxPrecision = 12; | ||
@@ -7,6 +7,3 @@ const rounding = (intStr, decimalStr, decimalLength, type) => { | ||
} | ||
if (intStr.length + decimalLength > maxDoubleLength) { | ||
return [intStr || '', (decimalStr || '').slice(0, decimalLength)]; | ||
} | ||
decimalLength = Math.max(decimalLength, 0); | ||
decimalLength = Math.max(Math.min(decimalLength, maxPrecision - intStr.length), 0); | ||
const handler = type === 'ceil' ? Math.ceil : (type === 'floor' ? Math.floor : Math.round); | ||
@@ -22,3 +19,3 @@ const rstStrs = | ||
* @param {number} [length] max display char length (better >= 5 to allow any number); default 9 | ||
* @param {number} [precision] max decimal precision; default equals length | ||
* @param {number} [decimal] max decimal precision; default equals length | ||
* @param {string} [placeholder] result when neither number nor text; default '' | ||
@@ -31,3 +28,3 @@ * @param {boolean} [allowText] allow text as results, if false text will convert to placeholder, text will be slice within length param; default true | ||
length = 9, | ||
precision, | ||
decimal, | ||
placeholder = '', | ||
@@ -38,4 +35,4 @@ allowText = true, | ||
} = {}) => value => { | ||
if (precision == null) { | ||
precision = length; | ||
if (decimal == null) { | ||
decimal = length; | ||
} | ||
@@ -53,3 +50,3 @@ placeholder = placeholder.slice(0, length); | ||
value = String(value); | ||
const cells = value.match(/^(-?)(\d*)(\.(\d+))?$/); | ||
let cells = value.match(/^(-?)(\d*)(\.(\d+))?$/); | ||
@@ -60,4 +57,7 @@ if (!cells || value === '' || value === '-') { | ||
preciseValue = String(parseFloat(parseFloat(value).toPrecision(maxPrecision))); | ||
cells = preciseValue.match(/^(-?)(\d*)(\.(\d+))?$/); | ||
const negative = cells[1]; | ||
let [int, deci] = rounding(cells[2] || '0', cells[4] || '', precision, roundingType); | ||
let [int, deci] = rounding(cells[2] || '0', cells[4] || '', decimal, roundingType); | ||
const localeInt = int.replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
@@ -64,0 +64,0 @@ |
{ | ||
"name": "number-display", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Display number smartly within a certain length.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,4 +19,11 @@ # number-display | ||
- when omitting decimals, you can change the rounding type, default to 'round' | ||
- no decimal trailing zeros | ||
- no decimal tailing zeros | ||
- no float error | ||
**Blogs**: | ||
[En](https://medium.com/front-end-weekly/displaying-numbers-in-frontend-2336323493c2) | ||
[中文](https://zhuanlan.zhihu.com/p/85536865) | ||
## Install | ||
@@ -97,3 +104,3 @@ | ||
**precision** | ||
**decimal** | ||
@@ -100,0 +107,0 @@ ( default: equals to 'length' ) |
10200
131
78