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

Update to handle small steps - ie: 0.0000001 #427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Update to handle small steps - ie: 0.0000001 #427

wants to merge 1 commit into from

Conversation

ebouwsema
Copy link

I was having issues with very, very small step increments. Updated so that the code now will handle small steps by multiplying by that step amount to convert to a handleable number.

Example:
var testObj = ko.observable('')
.extend({ step: 0.00000003 });

testObj(0.00001115);
    testObj.isValid() != true;

@gtripoli
Copy link

gtripoli commented Feb 12, 2020

Hi,
I solved it this way

validation.rules['step'] = {
        validator: function (value, step) {
            var values = (value).toString().split(".");
            var steps = (step).toString().split(".");

            if (kv.utils.isEmptyVal(val) || step === 'any') { return true; }

            if (values.length <= 1 && steps.length <= 1) {
                return (value % step) == 0;
            }

            var decimals = (values.length > 1) ? values[1].split("").length : steps[1].split("").length;

            var multiplier = 10 ** decimals;

            return Math.round(value * multiplier) % (step * multiplier) < 1
        },
        message: 'The value must increment by {0}.'
    };

https://jsfiddle.net/v5zm18s7/1/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants