WORDS2NUM converts a string with number value/s given in English words to a numeric vector: for example the string 'one thousand and twenty-four' is returned as 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 numeric vector's 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 / Knuth (-yllion).
### Bonus Functions / Scripts ###
* WORDS2NUM_TEST contains thousands of test cases.
* WORDS2NUM_DEMO compares the output of WORDS2NUM against real-world examples.
### Reverse Conversion ###
http://www.mathworks.com/matlabcentral/fileexchange/47221-number-to-words
### 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
Stephen Cobeldick (2021). Words to Number (https://www.mathworks.com/matlabcentral/fileexchange/52925-words-to-number), MATLAB Central File Exchange. Retrieved .
Inspired by: num2english, num2english, Variable Precision Integer Arithmetic, Number to Scientific Prefix, Date Vector/Number to ISO 8601 Date String, Customizable Natural-Order Sort, ISO 8601 Date String to Serial Date Number, Numeric to Ordinal-String, Number to Words, Natural-Order Row Sort, Natural-Order Filename Sort, Convert number to words, Number to Myriad
Inspired: Round to Electronic Component Values, Scientific Prefix to Number, Number to Scientific Prefix, Customizable Natural-Order Sort, Numeric to Ordinal-String, Number to Words, Natural-Order Row Sort, Natural-Order Filename Sort
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.