Skip to content

Commit

Permalink
Added percentage format
Browse files Browse the repository at this point in the history
  • Loading branch information
willijar committed Jan 6, 2012
1 parent 667a8a9 commit c915f00
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion defpackage.lisp
Expand Up @@ -38,7 +38,7 @@
#:invalid-format-type #:invalid-format-value #:invalid-format-reason
;; new use data types - ang and date are also formatter functions
#:date #:filename #:eng #:time-period #:pathnames #:separated #:roman
#:headers #:dimensional-parameter #:*timezone*
#:headers #:dimensional-parameter #:*timezone* #:percentage
;; functions for doing aggregates of user data and condition
#:parse-options #:parse-arguments #:ignore-extra-arguments #:use-default
;; some other more generally useful helper library functions
Expand Down
13 changes: 13 additions & 0 deletions validation.lisp
Expand Up @@ -994,3 +994,16 @@ only the suffix is output. If nil no units or suffix is output"
(defmethod equivalent((spec (eql 'dimensional-parameter)) input reference &rest rest)
(and (equal (cdr reference) (cdr input))
(apply #'equivalent `(number ,(car input) ,(car reference) ,@rest))))

(defmethod parse-input((spec (eql 'percentage)) (value string)
&key (min 0) (max 100) nil-allowed &allow-other-keys)
(parse-input 'number value :min min :max max :nil-allowed nil-allowed))

(defmethod format-output((spec (eql 'percentage)) num
&key (places 0) (%-p t))
"Return a percentage value formatted for user output (default 0 places)"
(if num
(if (= places 0)
(format nil "~D~@[%~]" (round num) %-p)
(format nil (format nil "~~,~DF~@[%~]" places %-p) num))
(when %-p "%")))

0 comments on commit c915f00

Please sign in to comment.