Created
August 16, 2022 10:28
-
-
Save andreasvirkus/109b0cf343486939b7d7897aaa81c0fc 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
const roundingStops = [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000] | |
export const roundToScale = (count, scale = roundingStops) => { | |
if (count === 0) return 0 | |
const nearestIdx = scale.findIndex((s) => s > count) | |
let nearest = scale[nearestIdx] | |
if (nearestIdx !== 0 && nearest / 2 > count) nearest = scale[nearestIdx - 1] || scale[0] | |
return nearest || count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment