formatMoney
formatMoney formats a monetary value based on your shop's currency settings. It's the JavaScript counterpart to Liquid's money filters and is essential whenever you're rendering prices client-side (variant switching, cart totals, etc.), since Shopify's AJAX API returns prices as an integer number of cents.
It accepts:
| Argument | Type | Default | Description |
|---|---|---|---|
cents | number \| string | — | The value in cents, e.g. 300 for £3.00. |
format | string | window.theme.moneyFormat | The shop's money_format string. Defaults to the value on the global theme object. |
note
By default formatMoney reads the format from window.theme.moneyFormat, so make sure that has been set (it's populated by the starter theme). You can always pass an explicit format string as the second argument.
Usage examples
Basic
- 2.0 Build
- 1.0 Build
// Assuming window.theme.moneyFormat === '£{{ amount }}'
window.BAO.utils.formatMoney(300) // '£3.00'
window.BAO.utils.formatMoney(1999) // '£19.99'
// Assuming window.theme.moneyFormat === '£{{ amount }}'
formatMoney(300) // '£3.00'
formatMoney(1999) // '£19.99'
Passing an explicit format
- 2.0 Build
- 1.0 Build
window.BAO.utils.formatMoney(250000, '${{ amount_with_comma_separator }}')
// '$2.500,00'
formatMoney(250000, '${{ amount_with_comma_separator }}')
// '$2.500,00'