aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/finance/money.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/world/finance/money.lux')
-rw-r--r--stdlib/source/library/lux/world/finance/money.lux23
1 files changed, 21 insertions, 2 deletions
diff --git a/stdlib/source/library/lux/world/finance/money.lux b/stdlib/source/library/lux/world/finance/money.lux
index 3d5903c8c..d53ccd80e 100644
--- a/stdlib/source/library/lux/world/finance/money.lux
+++ b/stdlib/source/library/lux/world/finance/money.lux
@@ -118,15 +118,34 @@
#amount (n.- (the #amount parameter)
(the #amount subject))])})))
+ (def (padded range it)
+ (-> Nat Nat
+ Text)
+ (let [range (%.nat (-- range))
+ it (%.nat it)
+
+ expected_digits (text.size range)
+ actual_digits (text.size it)]
+ (if (n.= expected_digits
+ actual_digits)
+ it
+ (loop (next [leading_zeroes (n.- actual_digits expected_digits)
+ it it])
+ (when leading_zeroes
+ 0 it
+ _ (next (-- leading_zeroes)
+ (%.format "0" it)))))))
+
(def .public (format it)
(All (_ currency)
(%.Format (Money currency)))
(let [[currency amount] (nominal.representation it)
- [macro micro] (n./% (/.sub_divisions currency) amount)]
+ range (/.sub_divisions currency)
+ [macro micro] (n./% range amount)]
(%.format (%.nat macro)
(when micro
0 ""
- _ (%.format "." (%.nat micro)))
+ _ (%.format "." (padded range micro)))
" " (/.alphabetic_code currency))))
)