-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TH-155-aura: Format product price in product page based on a unified …
…locale code (#55) Co-authored-by: Anas <93322743+bj-anas@users.noreply.github.com>
- Loading branch information
1 parent
3138b9a
commit 71a9cf8
Showing
3 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function formatCurrency(amount, currencySymbol, locale = 'en-US', usePrecision = false) { | ||
const shouldUsePrecision = !(amount % 1 === 0) || usePrecision; | ||
|
||
const formatter = new Intl.NumberFormat(locale, { | ||
style: 'decimal', | ||
minimumFractionDigits: shouldUsePrecision ? 2 : 0, | ||
}); | ||
|
||
const formattedValue = formatter.format(amount); | ||
|
||
const determineSymbolPositionFormatter = new Intl.NumberFormat(locale, { | ||
style: 'currency', | ||
currency: 'USD', | ||
currencyDisplay: 'symbol', | ||
}); | ||
|
||
const parts = determineSymbolPositionFormatter.formatToParts(1); // format with 1 USD just to determine the position of the currency symbol | ||
const symbolIndex = parts.findIndex(part => part.type === 'currency'); | ||
const isSymbolOnLeft = symbolIndex === 0; | ||
|
||
return isSymbolOnLeft | ||
? `${currencySymbol} ${formattedValue}` | ||
: `${formattedValue} ${currencySymbol}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters