Skip to content

Commit 6b5d7d9

Browse files
major units should also format currency (#1220)
* major units should also format currency * add changeset
1 parent 1b573af commit 6b5d7d9

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

.changeset/bitter-trains-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@godaddy/react": patch
3+
---
4+
5+
fix formatting for major units in formatCurrency

packages/react/src/components/checkout/utils/format-currency.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export interface FormatCurrencyOptions {
4747
/** Optional locale, defaults to 'en-US' */
4848
locale?: string;
4949
/**
50-
* Indicates whether the input is already in cents (minor units).
51-
* - true → format to currency string (default)
52-
* - false → convert to minor units and return as string
50+
* Indicates whether the input is in cents (minor units).
51+
* - true → input is in cents/minor units, will be converted to dollars/major units for formatting (default)
52+
* - false → input is already in dollars/major units, will be formatted as-is
5353
*/
5454
inputInMinorUnits?: boolean;
5555
/**
@@ -61,10 +61,10 @@ export interface FormatCurrencyOptions {
6161
}
6262

6363
/**
64-
* Formats or converts a currency amount.
64+
* Formats a currency amount.
6565
*
66-
* - When `inputInMinorUnits = true` (default): returns formatted string like "$123.45"
67-
* - When `inputInMinorUnits = false`: returns string representing minor units like "12345"
66+
* - When `inputInMinorUnits = true` (default): converts from minor units (cents) and returns formatted string like "$123.45"
67+
* - When `inputInMinorUnits = false`: formats major units (dollars) directly and returns formatted string like "$123.45"
6868
* - When `returnRaw = true`: returns numeric value without currency symbol like "123.45"
6969
*/
7070
export function formatCurrency({
@@ -78,13 +78,10 @@ export function formatCurrency({
7878

7979
const { precision = 2 } = config;
8080

81-
if (!inputInMinorUnits) {
82-
// Convert major units to minor units and return as string
83-
return Math.round(amount * Math.pow(10, precision)).toString();
84-
}
85-
86-
// Format value already in minor units
87-
const value = amount / Math.pow(10, precision);
81+
// Calculate the value to format
82+
const value = inputInMinorUnits
83+
? amount / Math.pow(10, precision) // Convert from minor units (cents) to major units (dollars)
84+
: amount; // Already in major units (dollars)
8885

8986
const nfLocale = returnRaw ? 'en-US' : locale;
9087

0 commit comments

Comments
 (0)