Fun Question but Confusing! Convert Number to English phrase

1 view (last 30 days)
d
  1 Comment
Stephen23
Stephen23 on 7 Dec 2015
Very easily using my FEX submission num2words:
Easy to use, plus options for customizing the format, case, scale, sig-figs, etc:
>> num2words(0)
ans = 'zero'
>> num2words(1024)
ans = 'one thousand and twenty-four'
>> num2words(-1024)
ans = 'negative one thousand and twenty-four'
>> num2words(1024, 'pos',true, 'case','title', 'hyphen',false)
ans = 'Positive One Thousand and Twenty Four'
>> num2words(1024, struct('type','ordinal', 'case','sentence'))
ans = 'One thousand and twenty-fourth'
>> num2words(1024, 'and',false, 'order',1) % round to the tens.
ans = 'one thousand twenty'

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Dec 2014
I would approach it this way:
nc = {'one' 'two' 'three' 'ten' 'fifteen' 'twenty'};
n = 23;
out = [nc{6} '-' nc{3}]
You will have to create your own full cell array and figure out how to parse the number and do the necessary cell array referencing, but this should get you started on one way to do it.

More Answers (2)

John D'Errico
John D'Errico on 4 Dec 2014
Edited: John D'Errico on 4 Dec 2014
I'd use vpi . It is on the file exchange. It has a nice toy in it that will convert any integer into valid english text, representing that number.
vpi2english(vpi('112242347947688988364501324523423523445'))
ans =
one hundred twelve undecillion, two hundred forty two decillion, three hundred forty seven nonillion, nine hundred forty seven octillion, six hundred eighty eight septillion, nine hundred eighty eight sextillion, three hundred sixty four quintillion, five hundred one quadrillion, three hundred twenty four trillion, five hundred twenty three billion, four hundred twenty three million, five hundred twenty three thousand, four hundred forty five
There are limits of course. I think I gave up at 10^306-1 as the largest number it would convert, mainly because I only found names up to that point when I looked online.
If you insist on doing it yourself, you can. Star gave you a simple answer that is quite valid and quite doable.

Sean de Wolski
Sean de Wolski on 4 Dec 2014

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!