File Exchange

image thumbnail

Words to Number

version 1.4 (15.6 KB) by

Convert English number name/s in a string to numeric value/s (GB/US).

19 Downloads

Updated

View License

Convert 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, uint or int.
* upper / lower / title / sentence case.
* the order of the multipliers: simple or compound.
* 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 English).
* whitespace character/s
* the numbering scale: short / long / Peletier / Rowlett / Knuth (-yllion).

### Convert Numbers to Words ###

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 million million, two thousand million, three million', 'mult','compound')
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 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

Comments and Ratings (0)

Updates

1.4

* Check for cell array optional inputs.

1.3

* Include HTML _DOC file in zip.

1.3

* Change HTML documentation filename.

1.3

* Rename "myriad" scale to "knuth".
* Change "mult" option 'ordered' to 'simple'.
* Improve fraction conversion algorithm.

1.2

* Add options <prefix> and <suffix>.
* Simplify multiplier parsing.
* Improve documentation.
* Consistent internal variable names.

1.1

* replace non-ASCII character with ASCII one in RegularExpression.

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video