Skip to content

Instantly share code, notes, and snippets.

@SanichKotikov
Created January 5, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanichKotikov/e49602ae3f353c8d9f15bf9cd9ebe733 to your computer and use it in GitHub Desktop.
Save SanichKotikov/e49602ae3f353c8d9f15bf9cd9ebe733 to your computer and use it in GitHub Desktop.
function calc(num: number, lines: number = 3) {
const step = (num + (num * 2 / 100)) / lines;
const shift = 10 ** (Math.ceil(Math.log10(Math.ceil(step) + 1)) - 2);
return Math.floor((Math.ceil(step / shift) * shift) * 10) / 10;
}
function round(num: number, low: number) {
return Math.max(calc(num), calc(low * -1));
}
console.log(round(0.4, 0));
console.log(round(0.6, -0.1));
console.log(round(1.1, -2.6));
console.log(round(2.5, -2.5));
console.log(round(5, -2));
console.log(round(5.9, -1));
console.log(round(15, -10));
console.log(round(34, -1));
console.log(round(99, 0));
console.log(round(380, -20));
console.log(round(3890, -1600));
console.log(round(38909, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment