Words to Number

Version 2.4.4 (47.1 KB) by Stephen23
Convert English number name/s written in a string to numeric value/s (GB/IN/US).
658 Downloads
Updated 8 Feb 2024

View License

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 / Indian / 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') % or "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('four billion', 'scale','short')
ans = 4000000000
>> words2num('four thousand million', 'scale','long')
ans = 4000000000
>> words2num('four arab', 'scale','indian')
ans = 4000000000
>> words2num('four milliard', 'scale','peletier')
ans = 4000000000
>> words2num('four gillion', 'scale','rowlett')
ans = 4000000000
>> words2num('FORTY MYLLION', 'scale','yllion')
ans = 4000000000
>> words2num('Negative Infinity')
ans = -Inf
>> words2num('Not-a-Number')
ans = NaN

Cite As

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

MATLAB Release Compatibility
Created with R2010b
Compatible with R2009b and later releases
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
2.4.4

* Documentation improvements.

2.4.3

* Add more testcases
* Add reference to SCU
* Add ARBSORT example

2.4.2

* option parsing improvements.
* add format reference.

2.4.1

* Add "IN" to summary.

2.4.0

* Faster option parsing.
* Add more testcases.
* Add "indian" scale.

2.3.1

* Improve option parsing.

2.3.0

* Add 3rd output: significant digits.
* Add 4th output: options structure.
* More testcases.

2.2.2

* Update description.

2.2.1

* Revise title and summary

2.2.0

* Accepts string or char vector input arguments.
* Add testcases.

2.1.0

* Add error IDs.
* Make compatible with R2009b, possibly earlier too (untested).

2.0.0

* Improve options handling.
* Rewrite regular expression: simpler and more accepting of non-standard numbers, uses nested functions called from dynamic regular expressions.

1.4.0.0

* Check for cell array optional inputs.

1.3.0.0

* Change HTML documentation filename.
* Rename "myriad" scale to "knuth".
* Change "mult" option 'ordered' to 'simple'.
* Improve fraction conversion algorithm.
* Include HTML _DOC file in zip.

1.2.0.0

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

1.1.0.0

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

1.0.0.0