Skip to content

Instantly share code, notes, and snippets.

@Godje
Created June 14, 2017 05:47
Show Gist options
  • Save Godje/80c9bb1b3f7993f87a9858d92696f18a to your computer and use it in GitHub Desktop.
Save Godje/80c9bb1b3f7993f87a9858d92696f18a to your computer and use it in GitHub Desktop.
Floor and Ceil to a certain digit. Javascript Function.
/*
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