home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # Name: numbers.icn
- #
- # Title: Format and convert numbers
- #
- # Author: Ralph E. Griswold and Tim Korb
- #
- # Date: December 27, 1989
- #
- ############################################################################
- #
- # These procedures format numbers in various ways:
- #
- # commas(s) inserts commas in s to separate digits into groups of
- # three.
- #
- # roman(i) converts s to Roman numerals.
- #
- # spell(i) spells out i in English.
- #
- # fix(i,j,w) formats i / j as a real (floating-point) number in
- # a field of width w with three digits to the right of
- # the decimal point, if possible.
- #
- ############################################################################
- #
- # Bug:
- #
- # The procedure fix() should be more general.
- #
- ############################################################################
-
- procedure commas(n)
- if *n < 4 then return n
- else return commas(left(n,*n - 3)) || map(",123","123",right(n,3))
- end
-
- # This procedure is based on a SNOBOL4 function written by Jim Gimpel.
- #
- procedure roman(n)
- local arabic, result
- static equiv
- initial equiv := ["","I","II","III","IV","V","VI","VII","VIII","IX"]
- integer(n) > 0 | fail
- result := ""
- every arabic := !n do
- result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1]
- if find("*",result) then fail else return result
- end
-
- procedure spell(n)
- local m
- n := integer(n) | stop(image(n)," is not an integer")
- if n <= 12 then return {
- "0zero,1one,2two,3three,4four,5five,6six,7seven,8eight,_
- 9nine,10ten,11eleven,12twelve," ? {
- tab(find(n))
- move(*n)
- tab(upto(","))
- }
- }
- else if n <= 19 then return {
- spell(n[2] || "0") ?
- (if ="for" then "four" else tab(find("ty"))) || "teen"
- }
- else if n <= 99 then return {
- "2twen,3thir,4for,5fif,6six,7seven,8eigh,9nine," ? {
- tab(upto(n[1]))
- move(1)
- tab(upto(",")) || "ty" ||
- if n[2] ~= 0 then "-" || spell(n[2])
- }
- }
- else if n <= 999 then return {
- spell(n[1]) || " hundred" ||
- (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
- }
- else if n <= 999999 then return {
- spell(n[1:-3]) || " thousand" ||
- (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
- }
- else if n <= 999999999 then return {
- spell(n[1:-6]) || " million" ||
- (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
- }
- else fail
- end
-
- procedure fix(i,j,w)
- /j := 1
- /w := 5
- if j = 0 then fail
- if w < 5 then w := 5
- r := real(i) / j
- if r < 0.001 then return repl(" ",w - 5) || "0.000"
- string(r) ? {
- int := tab(upto('.'))
- move(1)
- dec := tab(0)
- }
- return right(int,w - 4) || "." || left(dec,3,"0")
- end
-