Decimals
A decimal number parser and formatting package for Common Lisp.

The package can be installed through Quicklisp: (ql:quickload "decimals")

Link to the package source code and README file: DECIMALS.

This package introduces three public functions and one macro. See their own documentation for complete description. Here's a short introduction.


Function: PARSE-DECIMAL-NUMBER (string &key ...)

Examine a string for a decimal number and return it as a rational number.

DECIMALS> (parse-decimal-number "12,34" :decimal-separator #\,)
617/50

Function: FORMAT-DECIMAL-NUMBER (number &key ...)

Apply specified decimal number formatting rules to a number and return a formatted string.

DECIMALS> (format-decimal-number 20000/3 :round-magnitude -3
                                 :decimal-separator ","
                                 :integer-group-separator " "
                                 :integer-minimum-width 7)
"  6 666,667"
("" "6 666" "," "667")

DECIMALS> (loop for e from -5 upto 5
                do (print (format-decimal-number
                           (expt 10 e) :round-magnitude -5
                           :decimal-separator ","
                           :integer-minimum-width 7
                           :integer-group-separator " "
                           :fractional-minimum-width 7
                           :fractional-group-separator " ")))

"      0,000 01"
"      0,000 1 "
"      0,001   "
"      0,01    "
"      0,1     "
"      1       "
"     10       "
"    100       "
"  1 000       "
" 10 000       "
"100 000       "
NIL

Function: ROUND-HALF-AWAY-FROM-ZERO (number &optional (divisor 1))

A rounding function similar to CL:ROUND except that when number is exactly half-way between two integers round always away from zero. FORMAT-DECIMAL-NUMBER uses this rounding function by default.

Macro: DEFINE-DECIMAL-FORMATTER (name &body keyword-arguments)

Define a custom decimal number formatter function suitable for the ~/ directive of CL:FORMAT.


Tags: Library, Mathematics, Public Domain