27 lines
1009 B
Common Lisp
27 lines
1009 B
Common Lisp
;;; STRINGS 5: NUMBERS IN FORMAT
|
|
;;;
|
|
;;; ~a prints any value plainly. ~d and ~f print numbers with more
|
|
;;; care, and both take modifiers: a COLON on ~d groups the digits,
|
|
;;; (format nil "~:d" 1234567) => "1,234,567"
|
|
;;; and a number after a comma on ~f fixes the decimals,
|
|
;;; (format nil "~,2f" pi) => "3.14"
|
|
|
|
;; TODO: N widgets, grouped for humans: "1,234,567 widgets".
|
|
(defun price-tag (n)
|
|
(format nil "~d widgets" n))
|
|
|
|
;; TODO: LABEL is about N, with exactly two decimals:
|
|
;; "pi is about 3.14".
|
|
(defun fixed (label n)
|
|
(format nil "~a is about ~a" label n))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal "1,234,567 widgets" (price-tag 1234567))
|
|
(check-equal "7 widgets" (price-tag 7))
|
|
(check-equal "pi is about 3.14" (fixed "pi" pi))
|
|
(check-equal "e is about 2.72" (fixed "e" 2.718281828))
|