diff --git a/index.d.ts b/index.d.ts index 1ee09e5b4..0dd363805 100644 --- a/index.d.ts +++ b/index.d.ts @@ -136,6 +136,7 @@ export { getChildrenByType, getInitials, getSymbol, + getValueWithCurrency, GiftIcon, GlobeIcon, HandIcon, diff --git a/src/helpers/currency.js b/src/helpers/currency.js index 3f01210cc..771414cde 100644 --- a/src/helpers/currency.js +++ b/src/helpers/currency.js @@ -1,4 +1,5 @@ import getUserLocale from "get-user-locale"; +import { almostZero, numberFormat } from "../helpers/numbers"; const userLocale = getUserLocale(); @@ -18,3 +19,13 @@ export const getSymbol = (currency, locale = userLocale, amount = 0, isNarrowSym return string.replace(/\d/g, "").trim(); }; + +export const getValueWithCurrency = (amount, currency, locale) => { + const userLocale = locale ?? getUserLocale(); + + const amountVariable = almostZero(amount) ? 0 : amount; + const maxDigits = isZeroDecimal(currency) ? 0 : 2; + const formattedAmount = numberFormat(amountVariable, currency, userLocale, maxDigits, false).replace(".00", ""); + + return `${formattedAmount}`; +}; diff --git a/src/index.js b/src/index.js index ba2df7c9d..e52b24515 100644 --- a/src/index.js +++ b/src/index.js @@ -51,7 +51,7 @@ export { Number } from "./components/Utilities/Number"; // Helpers export { almostZero, numberFormat, roundNumber } from "./helpers/numbers"; -export { getSymbol, isZeroDecimal } from "./helpers/currency"; +export { getSymbol, getValueWithCurrency, isZeroDecimal } from "./helpers/currency"; export { formatDate, formatTime, dateFromObjectId } from "./helpers/date"; export { formatPhoneNumber } from "./helpers/phone"; export { isOSX, isIosBrowser } from "./helpers/browser";