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

floating point solution is added for multiple verification #2519

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/types/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,23 @@ module.exports = Any.extend({
},
validate(value, helpers, { base }, options) {

if (value % base === 0) {
if (floatSafeRemainder(value, base) === 0) {
return value;
}

function floatSafeRemainder(val, step){
//Check both is not floating number
if ( val % 1 === 0 && step % 1 === 0 ) {
return val % step;
}
var valDecCount = (val.toString().split('.')[1] || '').length;
var stepDecCount = (step.toString().split('.')[1] || '').length;
var decCount = valDecCount > stepDecCount? valDecCount : stepDecCount;
var valInt = parseInt(val.toFixed(decCount).replace('.',''));
var stepInt = parseInt(step.toFixed(decCount).replace('.',''));
return (valInt % stepInt) / Math.pow(10, decCount);
}

return helpers.error('number.multiple', { multiple: options.args.base, value });
},
args: [
Expand Down