필터 지우기
필터 지우기

Fun Question but Confusing! Convert Number to English phrase

조회 수: 1 (최근 30일)
Sang Yeob Kim
Sang Yeob Kim 2014년 12월 4일
댓글: Stephen23 2015년 12월 7일
d
  댓글 수: 1
Stephen23
Stephen23 2015년 12월 7일
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'

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2014년 12월 4일
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.

추가 답변 (2개)

John D'Errico
John D'Errico 2014년 12월 4일
편집: John D'Errico 2014년 12월 4일
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 2014년 12월 4일

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by