Skip to content

Instantly share code, notes, and snippets.

@ryanahamilton
Last active May 22, 2019 18:05
Show Gist options
  • Save ryanahamilton/03efbbc8b255324034af3bdc9dfea88b to your computer and use it in GitHub Desktop.
Save ryanahamilton/03efbbc8b255324034af3bdc9dfea88b to your computer and use it in GitHub Desktop.
// @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