From 782f339b9659c23653b7fadd1c1d80c6c2809e08 Mon Sep 17 00:00:00 2001 From: arnaud Date: Thu, 22 Jun 2017 15:38:36 +0200 Subject: [PATCH] Start to fix number with float values The edge number management has been disabled for now --- js/jcf.number.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/js/jcf.number.js b/js/jcf.number.js index ed15c97..6b4f781 100644 --- a/js/jcf.number.js +++ b/js/jcf.number.js @@ -106,6 +106,8 @@ jcf.addModule(function($) { diff = Math.abs(edgeNumber - newValue) % this.stepValue; // handle step diff + //For now do not manage edgeNumber + diff = 0; if (diff) { if (increment) { newValue += addValue - diff; @@ -113,7 +115,8 @@ jcf.addModule(function($) { newValue -= diff; } } else { - newValue += addValue; + //newValue += addValue; + newValue = Math.addFloats(newValue, addValue); } // handle min/max limits @@ -158,3 +161,25 @@ jcf.addModule(function($) { }); }(jcf)); + + +Math.addFloats = function (f1, f2) { + //Helper function to find the number of decimal places + function findDec(dec) { + var count = 0; + while (dec % 1) { + dec *= 10; + count++; + } + return count; + } + + //Determine the greatest number of decimal places + var dec1 = findDec(f1); + var dec2 = findDec(f2); + var fixed = dec1 > dec2 ? dec1 : dec2; + + //do the math then do a toFixed, could do a toPrecision also + var n = (f1 + f2).toFixed(fixed); + return +n; +}