# Difference between revisions of "Convert an integer into words"

### From CodeCodex

(→Algol 68) |
(Undo revision 6982 by 202.45.119.22 (talk)) |
||

Line 5: | Line 5: | ||

===Algol 68=== | ===Algol 68=== | ||

− | + | ||

<pre>PROC number words = | <pre>PROC number words = | ||

( | ( |

## Revision as of 04:58, 6 December 2010

The following code will turn integer numerals (eg. 1, 2, 3) into spelled out, words (eg. "one", "two", "three").

## Implementations

### Algol 68

PROC number words = ( INT n ) STRING : # returns a string representation of n in words. Currently deals with anything from 0 to 999 999 999. # BEGIN [] STRING digits = [] STRING ( "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" )[@0]; [] STRING teens = [] STRING ( "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" )[@0]; [] STRING decades = [] STRING ( "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" )[@2]; PROC three digits = ( INT n ) STRING : # does the conversion for n from 0 to 999. # BEGIN INT tens = n MOD 100 OVER 10; INT units = n MOD 10; IF n >= 100 THEN digits[n OVER 100] + " " + "hundred" + IF n MOD 100 /= 0 THEN " and " ELSE "" FI ELSE "" FI + IF tens /= 0 THEN IF tens = 1 THEN teens[units] ELSE decades[tens] + IF units /= 0 THEN "-" ELSE "" FI FI FI + IF units /= 0 AND tens /= 1 OR n = 0 THEN digits[units] ELSE "" FI END; INT n3 = n OVER 1 000 000; INT n2 = n MOD 1 000 000 OVER 1000; INT n1 = n MOD 1000; IF n3 /= 0 THEN three digits(n3) + " million" ELSE "" FI + IF n3 /= 0 AND (n2 /= 0 OR n1 >= 100) THEN ", " ELSE "" FI + IF n2 /= 0 THEN three digits(n2) + " thousand" ELSE "" FI + IF (n3 /= 0 OR n2 /= 0) AND n1 > 0 AND n1 < 100 THEN " and " ELIF n2 /= 0 AND n1 /= 0 THEN ", " ELSE "" FI + IF n1 /= 0 OR n = 0 THEN three digits(n1) ELSE "" FI END; DO # until user hits EOF # on logical file end(standin, (REF FILE f) BOOL : GOTO finish); on value error(standin, (REF FILE f) BOOL : GOTO finish); INT n; print("n? "); read((n, new line)); print((number words(n), new line)) OD; finish: SKIP

### Perl

<HIGHLIGHTSYNTAX language="perl"> use Lingua::EN::Numbers qw(num2en); num2en 234; </HIGHLIGHTSYNTAX>