From f8ee23b271a7f58ff4e8940e2c627ce4313799dc Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 3 Aug 2021 20:13:01 +0200 Subject: [PATCH] Fix: Precision got messed up --- index.html | 49 ++++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/index.html b/index.html index c8613d4..2578701 100644 --- a/index.html +++ b/index.html @@ -145,43 +145,26 @@ */ function dec (num, deci) { - let amount; - let result; - let missing; - let i; + const int = parseInt (num, 10); + const flo = parseFloat ((num % 1).toFixed(deci)); + const len = (String (flo).split ('.')[1] || '').length; + const diff = deci - len; + let res = String (int + flo); - // rounding - if (deci) { - amount = Math.pow (10, deci || 1); - result = Math.round (num * amount) / amount; - } - else { - result = Math.round (num); - } - - // force trailing zeros - result = String (result); - - if (!deci) { - return result; - } - - if (result.indexOf ('.') > -1) { - missing = deci; - result += '.'; - } - else { - missing = deci - result.split ('.')[1].length; - } - - if (missing) { - for (i = missing; i > 0; i--) { - result += '0'; + if (!flo && diff) { + res += '.'; + for (let i = 0; i < diff; i++) { + res += '0'; } } - // done - return result; + if (flo && diff > 0) { + for (let i = 0; i < diff; i++) { + res += '0'; + } + } + + return res; }