Convert Integer to (English) Words

This could be useful in a cheque-writing program.

/* integer_words(Integer, Words) is true if the positive Integer less   */
/*   than than 1 000 000 000 can be expressed by the (English) Words.   */
integer_words(Integer, Words):-
  Integer > 0,
  Integer < 1000000000,
  int(Integer, Strings, []),
  cat(Strings, Words, _).

int(I) --> 
  {I =< 19}, !, le19(I).
int(I) --> 
  {I =< 99}, !, {Q is I // 10, R is I mod 10}, le99(Q, R).
int(I) --> 
  {I =< 999}, !, {Q is I // 100, R is I mod 100}, le999(Q, R).
int(I) --> 
  {I =< 999999}, !, {Q is I // 1000, R is I mod 1000}, le999999(Q, R).
int(I) --> 
  {Q is I // 1000000, R is I mod 1000000}, le999999999(Q, R).

le19( 1) --> [`one`].
le19( 2) --> [`two`].
le19( 3) --> [`three`].
le19( 4) --> [`four`].
le19( 5) --> [`five`].
le19( 6) --> [`six`].
le19( 7) --> [`seven`].
le19( 8) --> [`eight`].
le19( 9) --> [`nine`].
le19(10) --> [`ten`].
le19(11) --> [`eleven`].
le19(12) --> [`twelve`].
le19(13) --> [`thirteen`].
le19(14) --> [`fourteen`].
le19(15) --> [`fifteen`].
le19(16) --> [`sixteen`].
le19(17) --> [`seventeen`].
le19(18) --> [`eighteen`].
le19(19) --> [`nineteen`].

le99(2, R) --> [`twenty`], le9(R).
le99(3, R) --> [`thirty`], le9(R).
le99(4, R) --> [`forty`], le9(R).
le99(5, R) --> [`fifty`], le9(R).
le99(6, R) --> [`sixty`], le9(R).
le99(7, R) --> [`seventy`], le9(R).
le99(8, R) --> [`eigthy`], le9(R).
le99(9, R) --> [`ninety`], le9(R).

le9(0) --> !.
le9(R) --> [` `], le19(R).

le999(Q, 0) --> !, le19(Q), [` hundred`].
le999(Q, R) --> le19(Q), [` hundred and `], int(R).

le999999(Q, 0) --> !, int(Q), [` thousand`].
le999999(Q, R) --> {R < 100}, !, int(Q), [` thousand and `], int(R).
le999999(Q, R) --> int(Q), [` thousand `], int(R).

le999999999(Q, 0) --> !, int(Q), [` million`].
le999999999(Q, R) --> {R < 100}, !, int(Q), [` million and `], int(R).
le999999999(Q, R) --> int(Q), [` million `], int(R).

LPA Index     Home Page