From c29c80c6e3601b6fbdd68133764637cf98a23860 Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Tue, 22 Jun 2021 13:22:53 +0100 Subject: [PATCH] Support underscores as separators --- README.md | 4 ++-- decimal.js | 7 +++++-- decimal.mjs | 5 ++++- doc/API.html | 1 + 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b7ed5a7..ebdc7d5 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ The methods that return a Decimal can be chained. ```js x.dividedBy(y).plus(z).times(9).floor() -x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil() +x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4_444_562_598.111772').ceil() ``` Many method names have a shorter alias. @@ -235,7 +235,7 @@ then npm run build ``` -will create *decimal.min.js*, and a source map will also be added to the *doc* directory. +will create *decimal.min.js* and a source map. ## Licence diff --git a/decimal.js b/decimal.js index 2d1eb8a..403c81a 100644 --- a/decimal.js +++ b/decimal.js @@ -3596,8 +3596,11 @@ */ function parseOther(x, str) { var base, Ctor, divisor, i, isFloat, len, p, xd, xe; - - if (str === 'Infinity' || str === 'NaN') { + + if (str.indexOf('_') > -1) { + str = str.replace(/(\d)_(?=\d)/g, '$1'); + if (isDecimal.test(str)) return parseDecimal(x, str); + } else if (str === 'Infinity' || str === 'NaN') { if (!+str) x.s = NaN; x.e = NaN; x.d = null; diff --git a/decimal.mjs b/decimal.mjs index 6d03387..0921a28 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -3593,7 +3593,10 @@ function parseDecimal(x, str) { function parseOther(x, str) { var base, Ctor, divisor, i, isFloat, len, p, xd, xe; - if (str === 'Infinity' || str === 'NaN') { + if (str.indexOf('_') > -1) { + str = str.replace(/(\d)_(?=\d)/g, '$1'); + if (isDecimal.test(str)) return parseDecimal(x, str); + } else if (str === 'Infinity' || str === 'NaN') { if (!+str) x.s = NaN; x.e = NaN; x.d = null; diff --git a/doc/API.html b/doc/API.html index 36b716e..51b3752 100644 --- a/doc/API.html +++ b/doc/API.html @@ -301,6 +301,7 @@ new Decimal('0xff.8') // '255.5' new Decimal(0.046875) // '0.046875' new Decimal('0.046875000000') // '0.046875' +new Decimal('0.046_875_000_000') // '0.046875' new Decimal(4.6875e-2) // '0.046875' new Decimal('468.75e-4') // '0.046875'