Currency: Difference between revisions

4,942 bytes added ,  15 days ago
added RPL
(→‎{{header|Rust}}: changing an f64 to a Currency is dangerous as the original has limited precision - need to add round() before casting to i64)
(added RPL)
 
(9 intermediate revisions by 5 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 1,911 ⟶ 1,984:
total price after tax : 23683000006.16
</pre>
=={{header|RPL}}==
Need for big integers.
{{works with|RPL|HP-49C}}
« →STR
'''IF''' LASTARG 9 ≤ '''THEN''' "0" SWAP + '''END'''
1 OVER SIZE 2 - SUB
LASTARG NIP 1 + DUP 1 + SUB
"$" ROT SIZE LASTARG "0" IFTE +
"." + SWAP +
» '<span style="color:blue">→CURR</span>' STO
« 100 * R→I *
DUP <span style="color:blue">→CURR</span> CLLCD 1 DISP .5 WAIT
» '<span style="color:blue">→PRICE</span>' STO
« DUPDUP FLOOR - EVAL →NUM
0.5 ≥ SWAP CEIL LASTARG FLOOR IFTE
» '<span style="color:blue">→RND</span>' STO
« 100 * R→I
OVER <span style="color:blue">→CURR</span> "TPBT" →TAG
UNROT OVER * 100000 / <span style="color:blue">→RND</span> DUP <span style="color:blue">→CURR</span> "Tax" →TAG
UNROT + <span style="color:blue">→CURR</span> "TPWT" →TAG
» '<span style="color:blue">TAX→</span>' STO
 
4000000000000000 5.50 <span style="color:blue">→PRICE</span>
2 2.86 <span style="color:blue">→PRICE</span> +
7.65 <span style="color:blue">TAX→</span>
{{out}}
<pre>
3: TPBT:"$22000000000000005.72"
2: Tax:"$1683000000000000.44"
1: TPWT:"$23683000000000006.16"
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'bigdecimal/util'
Line 1,981 ⟶ 2,089:
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cents = (&self.amount * BigInt::from(100)).to_integer();
write!(f, "${}.{:0>2}", &cents / 100, &cents % 100)
}
}
Line 2,186 ⟶ 2,294:
{{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,349:
=={{header|Wren}}==
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
 
var hamburgers = BigRat.new("4000000000000000")
1,150

edits