Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X2-10202 | added currency formatter #335

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export {
getChildrenByType,
getInitials,
getSymbol,
getValueWithCurrency,
GiftIcon,
GlobeIcon,
HandIcon,
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/currency.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getUserLocale from "get-user-locale";
import { almostZero, numberFormat } from "../helpers/numbers";

const userLocale = getUserLocale();

Expand All @@ -18,3 +19,13 @@ export const getSymbol = (currency, locale = userLocale, amount = 0, isNarrowSym

return string.replace(/\d/g, "").trim();
};

export const getValueWithCurrency = (amount, currency) => {
const userLocale = getUserLocale();
Copy link
Contributor

@manojx031 manojx031 Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pass additional param called locale (optional field) and incase locale is there can you use it, we will be using that once we deploy localisation story.

const userLocale = locale ?? getUserLocale();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done.


const amountVariable = almostZero(amount) ? 0 : amount;
const maxDigits = isZeroDecimal(currency) ? 0 : 2;
const formattedAmount = numberFormat(amountVariable, currency, userLocale, maxDigits, false).replace(".00", "");

return `${formattedAmount}`;
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading