Currency: Difference between revisions

3,791 bytes added ,  2 months ago
m
no edit summary
(→‎{{header|Rust}}: Correct the formatting of minor currency units (zero-padded) otherwise it prints $2.01 as $2.1)
mNo edit summary
(7 intermediate revisions by 4 users not shown)
Line 319:
total 23683000000000006.16
</pre>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> REM No need for BigNum library.
REM This language uses 80bit (10 bytes!) for real values internally.
Price = 4E15 * 5.50 + 2.0 * 2.86
Tax = Price * .0765
Total = Price + Tax
 
REM Number printing will use 2 decimal places and 21 positions zone
@%=&020215
PRINT "Price = $" Price
PRINT "Tax = $" Tax
PRINT "Total = $" Total</syntaxhighlight>
{{out}}
<pre>Price = $ 22000000000000005.72
Tax = $ 1683000000000000.44
Total = $ 23683000000000006.16</pre>
 
=={{header|Bracmat}}==
Line 552 ⟶ 569:
Total with tax: $23683000000000006.16
</pre>
 
=={{header|Common Lisp}}==
Let us just use the built-in and convenient rationals (which use two bignums for numerator and denominator).
 
<syntaxhighlight lang="lisp">(defun print-$ (rat &key (prefix "") (stream t))
(multiple-value-bind (dollars cents) (truncate rat)
(format stream "~A~D.~D~%" prefix dollars (round (* 100 cents)))))
 
(defun compute-check (order-alist tax-rate)
(let* ((total-before-tax
(loop :for (amount . price) in order-alist
:sum (* (rationalize price) amount)))
(tax (* (rationalize tax-rate) total-before-tax)))
(print-$ total-before-tax :prefix "Total before tax: ")
(print-$ tax :prefix "Tax: ")
(print-$ (+ total-before-tax tax) :prefix "Total with tax: ")))
 
(compute-check '((4000000000000000 . 5.5) (2 . 2.86)) 0.0765)</syntaxhighlight>
 
{{out}}
<pre>Total before tax: 22000000000000005.72
Tax: 1683000000000000.44
Total with tax: 23683000000000006.16</pre>
 
A reader macro can be used as extra nice icing on the cake:
<syntaxhighlight lang="lisp">(defun read-$ (stream char)
(declare (ignore char))
(let* ((str (with-output-to-string (out)
;; TODO: read integer, if dot, read dot and another integer
(loop :for next = (peek-char nil stream t nil t)
:while (or (digit-char-p next) (char= next #\.))
:do (write-char (read-char stream t nil t) out))))
(dot-pos (position #\. str))
(dollars (parse-integer str :end dot-pos))
(cents (if dot-pos
(/ (parse-integer str :start (+ dot-pos 1))
(expt 10 (- (length str) (+ dot-pos 1))))
0)))
(+ dollars cents)))
(set-macro-character #\$ #'read-$ t)
 
(defun print-$ (rat &key (prefix "") (stream t))
(multiple-value-bind (dollars cents) (truncate rat)
(format stream "~A~D.~D~%" prefix dollars (round (* 100 cents)))))
 
(defun compute-check (order-alist tax-rate)
(let* ((total-before-tax
(loop :for (amount . price) in order-alist
:sum (* price amount)))
(tax (* (rationalize tax-rate) total-before-tax)))
(print-$ total-before-tax :prefix "Total before tax: ")
(print-$ tax :prefix "Tax: ")
(print-$ (+ total-before-tax tax) :prefix "Total with tax: ")))
 
(compute-check '((4000000000000000 . $5.5) (2 . $2.86)) 0.0765)</syntaxhighlight>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 2,186 ⟶ 2,259:
{{out}}
net=22000000000000005.72, tax=1683000000000000.44, total=23683000000000006.16
 
=={{header|Unicon}}==
Solution takes advantage of Unicon's FxPt class as well as Unicon's operator overloading extension.
<syntaxhighlight lang="unicon">import math
 
procedure main()
n_burgers := 4000000000000000
n_shakes := 2
 
price := FxPt(5.50) * n_burgers + FxPt(2.86) * n_shakes
tax := (price * FxPt(7.65/100)).round(2)
total := price + tax
 
write(left("Price", 10), "$", right(price.toString(),21))
write(left("Tax", 10), "$", right(tax.toString(),21))
write(left("Total", 10), "$", right(total.toString(),21))
end</syntaxhighlight>{{out}}
<pre>Price $ 22000000000000005.72
Tax $ 1683000000000000.44
Total $ 23683000000000006.16</pre>
 
=={{header|VBA}}==
Line 2,221 ⟶ 2,314:
=={{header|Wren}}==
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
 
var hamburgers = BigRat.new("4000000000000000")