formatAmount wip

This commit is contained in:
Mikhail Mikheev 2019-10-02 13:25:46 +04:00
parent f909ddc699
commit 2d90d804a3
1 changed files with 20 additions and 10 deletions

View File

@ -1,25 +1,35 @@
// @flow
// This is pretty new so I'll leave the docs here
// https://v8.dev/features/intl-numberformat
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
// Locale is an empty array because we want it to use user's locale
const lt1000Formatter = new Intl.NumberFormat([], { maximumFractionDigits: 5 })
const lt10000Formatter = new Intl.NumberFormat([], { maximumFractionDigits: 4 })
const lt100000Formatter = new Intl.NumberFormat([], { maximumFractionDigits: 3 })
const lt1000000Formatter = new Intl.NumberFormat([], { maximumFractionDigits: 2 })
const lt10000000Formatter = new Intl.NumberFormat([], { maximumFractionDigits: 1 })
const lt1kFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 5 })
const lt10kFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 4 })
const lt100kFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 3 })
const lt1mFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 2 })
const lt10mFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 1 })
const lt100mFormatter = new Intl.NumberFormat([], { maximumFractionDigits: 0 })
const lt1tFormatter = new Intl.NumberFormat([], { notation: 'compact' })
export const formatAmount = (number: string | number) => {
let numberFloat = parseFloat(number)
if (numberFloat < 1000) {
numberFloat = lt1000Formatter.format(numberFloat)
numberFloat = lt1kFormatter.format(numberFloat)
} else if (numberFloat < 10000) {
numberFloat = lt10000Formatter.format(numberFloat)
numberFloat = lt10kFormatter.format(numberFloat)
} else if (numberFloat < 100000) {
numberFloat = lt100000Formatter.format(numberFloat)
numberFloat = lt100kFormatter.format(numberFloat)
} else if (numberFloat < 1000000) {
numberFloat = lt1000000Formatter.format(numberFloat)
numberFloat = lt1mFormatter.format(numberFloat)
} else if (numberFloat < 10000000) {
numberFloat = lt10000000Formatter.format(numberFloat)
numberFloat = lt10mFormatter.format(numberFloat)
} else if (numberFloat < 100000000) {
numberFloat = lt100mFormatter.format(numberFloat)
} else if (numberFloat < 1000000000) {
numberFloat = lt1tFormatter.format(numberFloat)
}
return numberFloat