home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / numbers.icn < prev    next >
Encoding:
Text File  |  1990-03-05  |  2.9 KB  |  104 lines

  1. ############################################################################
  2. #
  3. #    Name:    numbers.icn
  4. #
  5. #    Title:    Format and convert numbers
  6. #
  7. #    Author:    Ralph E. Griswold and Tim Korb
  8. #
  9. #    Date:    December 27, 1989
  10. #
  11. ############################################################################
  12. #
  13. #     These procedures format numbers in various ways:
  14. #
  15. #     commas(s)        inserts commas in s to separate digits into groups of
  16. #            three.
  17. #
  18. #     roman(i)        converts s to Roman numerals.
  19. #
  20. #     spell(i)        spells out i in English.
  21. #
  22. #     fix(i,j,w)    formats i / j as a real (floating-point) number in
  23. #            a field of width w with three digits to the right of
  24. #            the decimal point, if possible.
  25. #
  26. ############################################################################
  27. #
  28. #  Bug:
  29. #
  30. #     The procedure fix() should be more general.
  31. #
  32. ############################################################################
  33.  
  34. procedure commas(n)
  35.    if *n < 4 then return n
  36.    else return commas(left(n,*n - 3)) || map(",123","123",right(n,3))
  37. end
  38.  
  39. #  This procedure is based on a SNOBOL4 function written by Jim Gimpel.
  40. #
  41. procedure roman(n)
  42.    local arabic, result
  43.    static equiv
  44.    initial equiv := ["","I","II","III","IV","V","VI","VII","VIII","IX"]
  45.    integer(n) > 0 | fail
  46.    result := ""
  47.    every arabic := !n do
  48.       result := map(result,"IVXLCDM","XLCDM**") || equiv[arabic + 1]
  49.    if find("*",result) then fail else return result
  50. end
  51.  
  52. procedure spell(n)
  53.    local m
  54.    n := integer(n) | stop(image(n)," is not an integer")
  55.    if n <= 12 then return {
  56.       "0zero,1one,2two,3three,4four,5five,6six,7seven,8eight,_
  57.          9nine,10ten,11eleven,12twelve," ? {
  58.             tab(find(n))
  59.             move(*n)
  60.             tab(upto(","))
  61.             }
  62.       }
  63.    else if n <= 19 then return {
  64.       spell(n[2] || "0") ?
  65.          (if ="for" then "four" else tab(find("ty"))) || "teen"
  66.       }
  67.    else if n <= 99 then return {
  68.       "2twen,3thir,4for,5fif,6six,7seven,8eigh,9nine," ? {
  69.          tab(upto(n[1]))
  70.          move(1)
  71.          tab(upto(",")) || "ty" ||
  72.             if n[2] ~= 0 then "-" || spell(n[2])
  73.          }
  74.       }
  75.    else if n <= 999 then return {
  76.       spell(n[1]) || " hundred" ||
  77.          (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
  78.       }
  79.    else if n <= 999999 then return {
  80.       spell(n[1:-3]) || " thousand" ||
  81.          (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
  82.       }
  83.    else if n <= 999999999 then return {
  84.       spell(n[1:-6]) || " million" ||
  85.          (if (m := n[2:0]) ~= 0 then " and " || spell(m) else "")
  86.       }
  87.    else fail
  88. end
  89.  
  90. procedure fix(i,j,w)
  91.    /j := 1
  92.    /w := 5
  93.    if j = 0 then fail
  94.    if w < 5 then w := 5
  95.    r := real(i) / j
  96.    if r < 0.001 then return repl(" ",w - 5) || "0.000"
  97.    string(r) ? {
  98.       int := tab(upto('.'))
  99.       move(1)
  100.       dec := tab(0)
  101.       }
  102.    return right(int,w - 4) || "." || left(dec,3,"0")
  103. end
  104.