@livelike/react-native
Version:
LiveLike React Native package
15 lines (13 loc) • 580 B
text/typescript
export function abbrevNum(num: number) {
const unitSymbols = ['', 'k', 'M'];
const tier = (Math.log10(num) / 3) | 0;
if (tier == 0) return num;
const scaled = num / Math.pow(10, tier * 3);
// Check if number is whole or above 100k so '.0' doesn't get added.
const wholeOrThreeDigit = scaled % 1 === 0 || scaled >= 10 ? 0 : 1;
return scaled.toFixed(wholeOrThreeDigit) + unitSymbols[tier];
}
export function roundTo(num: number, decimalPlaces: number) {
const roundToFactor = Math.pow(10, decimalPlaces);
return Math.round(num * roundToFactor) / roundToFactor;
}