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

Input for type 'number' divisibleBy issue (2.99 not divisible by 0.01) #114

Open
romaleev opened this issue Jun 28, 2016 · 0 comments
Open

Comments

@romaleev
Copy link

romaleev commented Jun 28, 2016

Issue occurs with the following source for number=2.99 and divisibleBy=0.01, for other numbers works fine so far:
revalidator.validate({num: 2.99}, { properties: {num: {type: 'number', exclusiveMinimum: 0.98, exclusiveMaximum: 10, divisibleBy: 0.01, required: true, allowEmpty: false, messages: { allowEmpty: ' empty', exclusiveMinimum: ' > 0.98', exclusiveMaximum: ' < 10' } } } })

After investigation I found that an issue happens here

        case 'number':
          ...
          constrain('divisibleBy', value, function (a, e) {
            var multiplier = Math.max((a - Math.floor(a)).toString().length - 2, (e - Math.floor(e)).toString().length - 2);
            multiplier = multiplier > 0 ? Math.pow(10, multiplier) : 1;
            console.log(555, (a * multiplier), (e * multiplier))
            return (a * multiplier) % (e * multiplier) === 0 //<-----
            //for num === 3.99: 
          });
          break;

For 3.99 works fine:
(a * multiplier) // 3.99*10000000000000000 === 39900000000000000 /*divisible by*/ (e * multiplier) === 100000000000000

But for 2.99 - an error:
(a * multiplier) // 2.99*10000000000000000 === 29900000000000004 //<---!!! /*not divisible by*/ (e * multiplier) === 100000000000000

I'm using the latest Chrome 51.0.2704.103 (64-bit)

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

No branches or pull requests

1 participant