Otter
Snippets
Otter
Upstream Otter 1.0.1 Tested
Format Number as Currency
Format a numeric amount into a currency string with 2-decimal precision.
Architecture & Runtime Preview
Otter 1.0.1Zero-dependency Otter 1.0.1 snippet (Math & Numbers). Copy the parameterized template or run the self-contained example.
- 1Category: Math & Numbers
- 2Attribution: Otter Reusable Library (Official)
- 3Zero external pack dependencies — paste directly into any .ot file
Source Code (2 files)
to formatCurrency amount
parts are
amount
.
join parts with "" into amountStr
if amountStr contains "."
split amountStr by "." into decParts
whole is first of decParts
cents is last of decParts
if length of cents is 1
cents is cents plus "0"
.
outParts are
"$"
whole
"."
cents
.
join outParts with "" into result
return result
otherwise
outParts are
"$"
amountStr
".00"
.
join outParts with "" into result
return result
.
.
to formatCurrency amount
parts are
amount
.
join parts with "" into amountStr
if amountStr contains "."
split amountStr by "." into decParts
whole is first of decParts
cents is last of decParts
if length of cents is 1
cents is cents plus "0"
.
outParts are
"$"
whole
"."
cents
.
join outParts with "" into result
return result
otherwise
outParts are
"$"
amountStr
".00"
.
join outParts with "" into result
return result
.
.
formatted1 is formatCurrency 42.5
formatted2 is formatCurrency 100
say "Price 1:" formatted1
say "Price 2:" formatted2
if formatted1 is not "$42.50"
fail with "formatCurrency decimal mismatch: expected $42.50"
.
if formatted2 is not "$100.00"
fail with "formatCurrency integer mismatch: expected $100.00"
.
Installation & Usage
otter snippet inspect conv-number-format
Copy template.ot directly into your .ot source file, or run example.ot with 'otter run example.ot'.