Currency: Difference between revisions
Use decimal library
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Use decimal library) |
||
Line 1,422:
=={{header|OCaml}}==
Using the [https://
<syntaxhighlight lang="ocaml">
let () =▼
let open Decimal in (* bring all functions and operators into scope locally *)
let s = of_string in
let i = of_int in
let
let
let tax_rate = s "7.65e-2" in
let subtotal = hamburgers + milkshakes in
let tax = subtotal * tax_rate in
let total = subtotal + tax in
Printf.printf
"Subtotal: %20s
Tax: %20s
Total: %20s\n"
(to_string (round ~n:2 subtotal))
(to_string (round ~n:2 tax))
(to_string (round ~n:2 total))
▲let () =
▲;;</syntaxhighlight>
{{out}}
<pre>
▲before tax: 22000000000000005.72
▲tax: 1683000000000000.44
▲total: 23683000000000006.16
</pre>
|