Search code examples
maxima

Convert numerals to words in Maxima


I'm trying to convert numerals to their English equivalent. For example, the string '15' would be converted to 'fifteen'.

I found this code sniipet that seems to work in the Maxima manual but it uses file processing functions:

(%i1) s: openw("E:/file.txt");
(%o1)                     #<output stream E:/file.txt>
(%i2) control: 
 "~2tAn atom: ~20t~a~%~2tand a list: ~20t~{~r ~}~%~2t\
and an integer: ~20t~d~%"$
(%i3) printf( s,control, 'true,[1,2,3],42 )$
(%o3)                                false
(%i4) close(s);
(%o4)                                true
(%i5) s: openr("E:/file.txt");
(%o5)                     #<input stream E:/file.txt>
(%i6) while stringp( tmp:readline(s) ) do print(tmp)$
  An atom:          true 
  and a list:       one two three  
  and an integer:   42 
 (%i7) close(s)$

There must be an easier and more straightforward way without using files.


Solution

  • printf spells out numbers as words using the ~r format directive. You can get a string from printf(false, ...), or you can print to the console with printf(true, ...). See ? printf for more information.

    (%i1) printf (true, "~r~%", 15);
    fifteen
    (%o1)                                false
    (%i2) my_string : printf (false, "~r", 15);
    (%o2)                               fifteen
    (%i3) my_string;
    (%o3)                               fifteen