필터 지우기
필터 지우기

integers to English phrase

조회 수: 2 (최근 30일)
Britnie Casillas
Britnie Casillas 2019년 11월 15일
댓글: Walter Roberson 2019년 11월 16일
I am trying to create a function that converts double digits between 21 and 39 into its english phrase.
I think I have the beginning of the code that sets the integer parameter but I do not know what to do next. This is what I have so far:
function S = Convert_to_words(number)
ones = {'', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'};
tens = {'', '', 'ERROR', 'Twenty', 'Thirty'};
end
  댓글 수: 10
Britnie Casillas
Britnie Casillas 2019년 11월 16일
wouldnt the tens_digits and one_digits be the same variables as the ones and tens? What do you mean derive them from number?
Stephen23
Stephen23 2019년 11월 16일
"wouldnt the tens_digits and one_digits be the same variables as the ones and tens? "
No.
tens ans ones are cell arrays of character vectors.
tens_digits and one_digits should be numeric values of the digits in the given number (which by adding one to (because MATLAB indexing starts from one) can be used as indices into ones and tens).
"What do you mean derive them from number? "
You need to get the digits from number, as numeric values.

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

채택된 답변

Matt J
Matt J 2019년 11월 16일
As a further hint, consider the following version, which is almost what you want
function S = convert_to_words(number)
ones= {'', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'};
tens = {'','ERROR', 'Twenty-', 'Thirty-'};
tens_digit=3;
ones_digit=4;
S=[tens{tens_digit + 1}, ones{ones_digit + 1}];
end
It works very reliably on the number 34,
>> S=convert_to_words(34)
S =
'Thirty-Four'
Your job is to get it to work for everything else.
  댓글 수: 2
Britnie Casillas
Britnie Casillas 2019년 11월 16일
Thank you very much
Walter Roberson
Walter Roberson 2019년 11월 16일
However 30 exactly is going to output something that ends in a hyphen when it shouldn't.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by