currency.js 821 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * This file is duplicated from services/web/frontend/js/shared/utils/currency.ts
  3. */
  4. /**
  5. * @import { CurrencyCode } from '../../../types/currency-code'
  6. */
  7. /**
  8. * @param {number} amount
  9. * @param {CurrencyCode} currency
  10. * @param {string} locale
  11. * @param {boolean} stripIfInteger
  12. * @returns {string}
  13. */
  14. function formatCurrencyLocalized(amount, currency, locale, stripIfInteger) {
  15. const options = { style: 'currency', currency }
  16. if (stripIfInteger && Number.isInteger(amount)) {
  17. options.minimumFractionDigits = 0
  18. }
  19. try {
  20. return amount.toLocaleString(locale, {
  21. ...options,
  22. currencyDisplay: 'narrowSymbol',
  23. })
  24. } catch {}
  25. try {
  26. return amount.toLocaleString(locale, options)
  27. } catch {}
  28. return `${currency} ${amount}`
  29. }
  30. module.exports = {
  31. formatCurrencyLocalized,
  32. }