Words to Number

versión 2.2.2 (38.9 KB) por Stephen23
Convert English number name/s written in a string to numeric value/s (GB/US).

606 descargas

Actualizada 31 Jan 2022

Ver licencia

WORDS2NUM converts text with number value/s written in English words to a numeric vector: for example the text 'one thousand and twenty-four' is converted to the scalar numeric 1024. WORDS2NUM accepts a string containing zero or more number substrings, and returns a numeric vector with one element for each detected number substring. Options allows the user to select:
  • the output vector's numeric class: double, single, uintX or intX.
  • upper / lower / title / sentence case.
  • the use of spaces between words.
  • the use of a comma between magnitude groups.
  • the use of a hyphen between tens and ones.
  • the use of 'and' before the tens/ones (required in British and Commonwealth English).
  • whitespace character/s.
  • the numbering scale: short / long / Peletier / Rowlett / yllion.
Bonus Functions / Scripts
  • WORDS2NUM_TEST contains thousands of test cases.
  • WORDS2NUM_DEMO compares the output of WORDS2NUM against real-world examples.
Reverse Conversion
### Examples ###
>> words2num("zero")
ans = 0
>> words2num('One Thousand and TWENTY-four')
ans = 1024
>> words2num('One_Thousand_and_TWENTY-four', 'white','_')
ans = 1024
>> words2num('One Thousand and TWENTY-four', 'case','lower')
ans = 4
>> words2num('One Thousand and TWENTY-four', 'case','upper')
ans = 20
>> words2num('One Thousand and TWENTY-four', 'case','title')
ans = 1000
>> words2num('One Thousand and TWENTY-four', 'hyphen',false)
ans = [1020,4]
>> words2num('One Thousand and TWENTY-four', 'and',false)
ans = [1000,24]
>> words2num('One Thousand and TWENTY-four', 'suffix','-')
ans = 1020
>> [num,spl] = words2num('Negative thirty-two squared is one thousand and twenty-four.')
num = [-32,1024]
spl = {'',' squared is ','.'}
>> [num,spl] = words2num('one hundred and twenty-three pounds and forty-five pence')
num = [123,45]
spl = {'',' pounds and ',' pence'}
>> [num,spl] = words2num('pi=threepointonefouronefiveninetwosixfivethreefiveeight')
num = 3.14159265358
spl = {'pi=',''}
>> [num,spl] = words2num('One Hundred and One Dalmatians')
num = 101
spl = {'',' Dalmatians'}
>> words2num('one hundred and seventy-nine uncentillion')
ans = 1.79e+308
>> words2num('one hundred and eighty uncentillion') % >realmax
ans = Inf
>> words2num('one hundred and eighty uncentillion', 'class','int64')
ans = +9223372036854775807
>> words2num(num2words(intmin('int64')),'class','int64')
ans = -9223372036854775808
>> words2num('one point zero zero two zero zero three trillion')
ans = 1002003000000
>> words2num('one trillion, two billion, three million')
ans = 1002003000000
>> words2num('one trillion, two billion three million', 'comma',true, 'and',true)
ans = [1002000000000,3000000]
>> words2num('one trillion, two billion three million', 'comma',false)
ans = [1000000000000,2003000000]
>> words2num('one million million', 'mag','compound')
ans = 1000000000000
>> words2num('one billion', 'scale','short')
ans = 1000000000
>> words2num('one thousand million', 'scale','long')
ans = 1000000000
>> words2num('one milliard', 'scale','peletier')
ans = 1000000000
>> words2num('one gillion', 'scale','rowlett')
ans = 1000000000
>> words2num('ten myllion', 'scale','knuth')
ans = 1000000000
>> words2num('Negative Infinity')
ans = -Inf
>> words2num('Not-a-Number')
ans = NaN

Citar como

Stephen23 (2022). Words to Number (https://www.mathworks.com/matlabcentral/fileexchange/52925-words-to-number), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2010b
Compatible con la versión R2009b y siguientes
Compatibilidad con las plataformas
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!