Last active
May 22, 2019 18:05
-
-
Save ryanahamilton/03efbbc8b255324034af3bdc9dfea88b 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
// @flow | |
const formatThousandsToK = (val: number | string | null): string => { | |
const num = Number(val) || 0; | |
const absNum = Math.abs(num); | |
const signNum = Math.sign(num); | |
if (absNum > 999) { | |
const reduced = signNum * (absNum / 1000); | |
return `${reduced.toFixed(1)}k`; | |
} else { | |
return (signNum * absNum).toString(); | |
} | |
}; | |
export default formatThousandsToK; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment