Created
June 14, 2017 05:47
-
-
Save Godje/80c9bb1b3f7993f87a9858d92696f18a to your computer and use it in GitHub Desktop.
Floor and Ceil to a certain digit. Javascript Function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
let floored = floorNum(1430); //1000 | |
let floored2 = floorNum(1430, 2); //1400 | |
let ceiled = ceilNum(1430); //2000 | |
let ceiled3 = ceilNum(1543, 3); //1550 | |
*/ | |
function floorNum(num, ind){ | |
ind = ind || 1; | |
let str = Math.floor(num)+"", | |
strL = str.length - (ind - 1), | |
arr = new Array(strL).fill("0"); | |
arr[0] = "1"; | |
let div = arr.join(""); | |
return Math.floor(num/div)*div; | |
} | |
function ceilNum(num, ind){ | |
ind = ind || 1; | |
let str = Math.floor(num)+"", | |
strL = str.length - (ind - 1), | |
arr = new Array(strL).fill("0"); | |
arr[0] = "1"; | |
let div = arr.join(""); | |
return Math.ceil(num/div)*div; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment