Data update
This commit is contained in:
parent
5af6d93694
commit
796d366b97
455 changed files with 7413 additions and 1900 deletions
14
Task/Currency/Common-Lisp/currency-1.lisp
Normal file
14
Task/Currency/Common-Lisp/currency-1.lisp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(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)
|
||||
30
Task/Currency/Common-Lisp/currency-2.lisp
Normal file
30
Task/Currency/Common-Lisp/currency-2.lisp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
(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)
|
||||
|
|
@ -49,7 +49,7 @@ impl Mul<u64> for Currency {
|
|||
impl fmt::Display for Currency {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let cents = (&self.amount * BigInt::from(100)).to_integer();
|
||||
write!(f, "${}.{}", ¢s / 100, ¢s % 100)
|
||||
write!(f, "${}.{:0>2}", ¢s / 100, ¢s % 100)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ impl Currency {
|
|||
|
||||
fn new(num: f64) -> Self {
|
||||
Self {
|
||||
amount: BigRational::new(((num * 100.0) as i64).into(), 100.into())
|
||||
amount: BigRational::new(((num * 100.0).round() as i64).into(), 100.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue