Skip to content

Instantly share code, notes, and snippets.

@aalfiann
Last active December 11, 2018 18:32
Show Gist options
  • Save aalfiann/62a582393276f8e1e74310401e779c00 to your computer and use it in GitHub Desktop.
Save aalfiann/62a582393276f8e1e74310401e779c00 to your computer and use it in GitHub Desktop.
Round decimal value with custom nearest point number (Pure JS)
/**
* Round decimal value with custom nearest point number
*
* @param numToRound is the value number to round
* @param pointDecimal is the nearest point
* @return decimal
*/
function limitRound(numToRound,pointDecimal){
pointDecimal=(pointDecimal===undefined)?0.5:pointDecimal;
var value = 0;
if((numToRound - Math.floor(numToRound)) >= pointDecimal){
value = 1;
} else {
value = 0;
}
return Math.floor(numToRound + value);
}
/* Usage how to use */
var number = 1.33;
console.log('Number: '+number);
console.log('Result (0.1): '+limitRound(number,0.1));
console.log('Result (0.2): '+limitRound(number,0.2));
console.log('Result (0.25): '+limitRound(number,0.25));
console.log('Result (0.33): '+limitRound(number,0.33));
console.log('Result (0.45): '+limitRound(number,0.45));
console.log('Result (0.50): '+limitRound(number));
console.log('Result (0.60): '+limitRound(number,0.60));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment