Skip to content

Instantly share code, notes, and snippets.

@hogart
Last active October 3, 2015 04:37
Show Gist options
  • Save hogart/2392803 to your computer and use it in GitHub Desktop.
Save hogart/2392803 to your computer and use it in GitHub Desktop.
quantize teturn nearest bigger to `value` number, which is multiple of `quant`
/**
* Return nearest bigger (by default, or lesser if the third parameter is true) to `value` number, which is multiple of `quant`
* @throws {Error} 'Illegal quantazing value'
* @param {Number} value
* @param {Number} quant must not be zero
* @param {Boolean} [toLower=false] return bigger rather than lesser nearest number
* @return {Number}
*/
function quantize(value, quant, toLower) {
if (quant === 0) {
throw new Error('Illegal quantazing value')
}
var operation = Math[toLower ? 'floor' : 'ceil'];
return operation( value / quant ) * quant;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment