Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to fix number with float values #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion js/jcf.number.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,17 @@ 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;
} else {
newValue -= diff;
}
} else {
newValue += addValue;
//newValue += addValue;
newValue = Math.addFloats(newValue, addValue);
}

// handle min/max limits
Expand Down Expand Up @@ -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;
}