numbers.ts 641 B

1234567891011121314151617181920212223242526272829303132
  1. import { defaultLocale } from "../data/locales"
  2. export const formatNumber = (
  3. number: number,
  4. localeCode: string = defaultLocale,
  5. options: Intl.NumberFormatOptions = {}
  6. ): string => {
  7. let intlOptions: Intl.NumberFormatOptions = {
  8. notation: "compact",
  9. ...options,
  10. }
  11. return new Intl.NumberFormat(localeCode, intlOptions).format(number)
  12. }
  13. export const percIncrease = (a: number, b: number): number => {
  14. let percent
  15. if (b !== 0) {
  16. if (a !== 0) {
  17. percent = (b - a) / a
  18. } else {
  19. percent = 1
  20. }
  21. } else if (b === 0 && a === 0) {
  22. percent = 0
  23. } else {
  24. percent = -1
  25. }
  26. return percent
  27. }