Created
January 5, 2020 11:49
-
-
Save SanichKotikov/e49602ae3f353c8d9f15bf9cd9ebe733 to your computer and use it in GitHub Desktop.
This file contains 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
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