Filter löschen
Filter löschen

integers to English phrase

2 Ansichten (letzte 30 Tage)
Britnie Casillas
Britnie Casillas am 15 Nov. 2019
Kommentiert: Walter Roberson am 16 Nov. 2019
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 Kommentare
Britnie Casillas
Britnie Casillas am 16 Nov. 2019
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 am 16 Nov. 2019
"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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 16 Nov. 2019
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 Kommentare
Britnie Casillas
Britnie Casillas am 16 Nov. 2019
Thank you very much
Walter Roberson
Walter Roberson am 16 Nov. 2019
However 30 exactly is going to output something that ends in a hyphen when it shouldn't.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by