Money & currency¶
A money value is an amount plus a currency. money() formats the pair using the
locale's symbol, placement, grouping, and — crucially — the currency's own minor
units: most currencies show two fraction digits, JPY shows none, and a few (e.g.
BHD) show three. Cosmo only formats a value; it never converts between currencies.
For plain numbers, percentages, and units, see Numbers.
Money is not currency conversion
money(100, "AUD") formats 100 AUD in the locale's style. It does not
convert 100 of anything into AUD — do the conversion yourself first, then format
the result. Cosmo bundles no exchange-rate data.
money() at a glance¶
| Argument | Default | Meaning |
|---|---|---|
value |
— | The numeric amount (already in the target currency). |
code / currency |
the currency modifier |
ISO 4217 code, e.g. "AUD", "JPY". |
precision |
currency's minor units | Override fraction digits (sets both min and max). |
strict |
false |
Throw instead of returning "" when no currency is resolved. |
options |
{} |
Any number-formatting option. |
In PHP the signature is positional —
money($value, $currency, $precision, $strict, $pattern, $options) — and adds a
raw ICU $pattern string. In JS/Python precision and strict live inside the
options object; in Java they are passed through the options Map. In C# they are
separate named parameters: Money(value, code, precision, strict, options).
Formatting an amount¶
How the currency is resolved¶
money() looks for a currency in this order, taking the first that is set:
- an explicit code passed to the call;
- the
currencymodifier set on the instance at construction; - (PHP, Python, Java, C# only) the locale's region, mapped to its currency.
The symbol is always the locale's disambiguated form — en_US writes Australian
dollars as A$, not the ambiguous $ — so amounts in different currencies never
collide visually. Amounts are rounded to the currency's minor units with the
halfExpand default.
Region → currency inference differs
PHP, Python, Java, and C# infer the currency from the locale's region when you
omit a code (Cosmo("en_AU").money(100) → $100.00). JavaScript does not —
its Intl-only design forbids a region→currency mapping, so money() returns
"" unless you pass a code or set the currency modifier. This is a capability
difference, not a bug; see Platform notes.
Setting a default currency once¶
If every amount in a context uses the same currency, set it as a modifier and drop the per-call code:
Overriding precision and grouping¶
precision overrides the currency's natural minor units; the rest of the
number options apply too. A common case is
showing a whole-dollar summary without cents:
Strict mode: failing loudly on a missing currency¶
By default money() returns "" when it cannot resolve a currency — handy in a
template, dangerous in a payment path. Pass strict to turn the silent empty
string into a thrown InvalidArgumentException:
Currency names & symbols¶
To display a currency on its own — its localised name or symbol, with no
amount attached — use currency():
currency() is part of the wider locale metadata
family (alongside language(), country(), and friends) — that page covers it in
full, including the strict-symbol behaviour. For a low-to-high money range
("$3.00 – $5.00"), see moneyRange().
Practical examples¶
An invoice line. Format the unit price, quantity, and line total with one currency-bound instance, and show the currency name in the footer:
Round-trip with the parser. money() formats; parseMoney()
(PHP/Python/Java/C#) reads a formatted string back into amount + currency — useful for
importing user-entered values.