react-native-modern-elements
Version:
A modern, customizable UI component library for React Native
23 lines (22 loc) • 638 B
JavaScript
const formatPrice = (value, decimals = 1) => {
const format = (num, suffix) => {
const fixed = +num.toFixed(decimals); // convert to number to remove unnecessary .0
return `${fixed}${suffix}`;
};
if (value >= 1000000000000) {
return format(value / 1000000000000, "T");
}
else if (value >= 1000000000) {
return format(value / 1000000000, "B");
}
else if (value >= 1000000) {
return format(value / 1000000, "M");
}
else if (value >= 1000) {
return format(value / 1000, "k");
}
else {
return `${value}`;
}
};
export default formatPrice;