Skip to content

Instantly share code, notes, and snippets.

View ravid7000's full-sized avatar
🎯
Focusing

Ravi Dhiman ravid7000

🎯
Focusing
View GitHub Profile
@ravid7000
ravid7000 / ceil-to-nearest.js
Created April 23, 2018 13:49
Find closest number multiple of 10, 100, 1000, 10000,...
/*
ceilToNearest(5)
> 10
*/
function ceilToNearest(num) {
let len = Math.ceil(Math.log10(Math.abs(num) + 1))
let divisor = Math.pow(10, (len - 1) === 0 ? 1 : (len - 1))
if (num % divisor > 0) {
return (Math.round(num / divisor) + 1) * divisor