Number to Words

Versione 4.1.4 (47,2 KB) da Stephen23
Convert a numeric to a string with the English name of the number value (GB/IN/US).
2,5K download
Aggiornato 8 feb 2024

Visualizza la licenza

Nota dell'editore: This file was selected as MATLAB Central Pick of the Week

NUM2WORDS converts a numeric scalar to text with the number value written in English words: for example the numeric scalar 1024 is returned as the text 'one thousand and twenty-four'. NUM2WORDS accepts a scalar of any standard numeric class: single, double, intX, or uintX. Integer types are parsed with their full precision, while floating types have internal precision limits to ensure the least-unexpected output. Options allows the user to select:
  • the output class: char or string.
  • the number type selection: ordinal / decimal / cheque / money / highest magnitude.
  • the number of significant digits or order-of-magnitude.
  • upper / lower / title / sentence case.
  • trailing zeros or without.
  • 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).
  • a 'positive' prefix for non-negative values.
  • the numbering scale: short / long / Indian / Peletier / Rowlett / yllion.
  • currency unit names, e.g. Pound, Pence, Dollar, Cents, Rupees, etc.
Bonus Functions / Scripts
  • NUM2WORDS_TEST contains thousands of test cases.
  • NUM2WORDS_DEMO compares the output of NUM2WORDS against real-world examples.
  • NUM2WORDS_RAT converts a numeric to a string with an improper fraction in words.
Reverse Conversion
Examples
>> num2words(0)
ans = 'zero'
>> num2words(1024)
ans = 'one thousand and twenty-four'
>> num2words(-1024)
ans = 'negative one thousand and twenty-four'
>> num2words(1024, 'pos',true, 'case','title', 'hyphen',false)
ans = 'Positive One Thousand and Twenty Four'
>> num2words(1024, struct('type','ordinal', 'case','sentence'))
ans = 'One thousand and twenty-fourth'
>> num2words(1024, 'and',false, 'order',1) % round to the tens.
ans = 'one thousand twenty'
>> num2words(pi, 'order',-10) % round to tenth decimal digit
ans = 'three point one four one five nine two six five three six'
>> num2words(intmax('uint32'), 'sigfig',4, 'type','highest')
ans = 'four point two nine five billion'
>> num2words(intmax('uint32'), 'sigfig',4, 'comma',false)
ans = 'four billion two hundred and ninety-five million'
>> num2words(intmax('uint32'), 'sigfig',4, 'scale','long')
ans = 'four thousand, two hundred and ninety-five million'
>> num2words(intmax('uint32'), 'sigfig',4, 'case','title', 'scale','indian')
ans = 'Four Arab, Twenty-Nine Crore, Fifty Lakh'
>> num2words(intmax('uint32'), 'order',6, 'case','upper', 'scale','yllion')
ans = 'FORTY-TWO MYLLION NINETY-FIVE HUNDRED MYRIAD'
>> num2words(1234.56, 'type','cheque', 'unit','Euro')
ans = 'one thousand, two hundred and thirty-four euro and fifty-six cents'
>> num2words(1234.56, 'type','cheque', 'unit','Pound|', 'subunit','Penny|Pence')
ans = 'one thousand, two hundred and thirty-four pounds and fifty-six pence'
>> num2words(101, 'type','money', 'unit','Dalmatian|', 'case','title')
>> num2words(1001, 'type','money', 'unit','Night|', 'case','title')
>> sprintf('%s Under the Sea',num2words(2e4, 'type','money', 'unit','League|', 'case','title'))

Cita come

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

Compatibilità della release di MATLAB
Creato con R2010b
Compatibile con R2009b e release successive
Compatibilità della piattaforma
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release
4.1.4

* Documentation improvements.

4.1.3

* FEX crashed while uploading the previous version... 2nd attempt.

4.1.2

* Add more testcases
* Add reference to SCU

4.1.1

* option parsing improvements.
* add format reference.

4.1.0

* Faster option parsing.
* Add more testcases.

4.0.1

* Improve option parsing.

4.0.0

* Major code refactoring.
* Indian scale up to Shankh.
* Add 2nd output: options structure.
* More testcases.

3.6.2

* Update description.

3.6.1

* Revise title and summary

3.6.0

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

3.5.1

* Add error IDs.

3.5.0

* Improve options handling.

3.4.0.0

* Check for cell array optional inputs.

3.3.0.0

* Include HTML _DOC file in zip.
* Change HTML documentation filename.
* Rename "myriad" scale to "knuth".
* Add published HTML documentation.
* Add HTML directory

3.2.0.0

* Improve documentation.
* Consistent internal variable names.

3.1.0.0

* Update license.

3.0.0.0

* Add 'india' number scale (lakh and crore).
* Fix bugs in rounding of integer class numerics (for large sigfigs).

2.2.0.0

* Simplify cell array creation.

2.1.0.0

* Fix bug in myriad parsing (if >1 highest level multiplier).
* More robust fraction handling for all number scales.
* Smaller file-size.

2.0.0.0

* Add a new <scale> option <myriad> (Knuth's -yllion).
* Rename option <ae> as <and>.

1.9.0.0

* More robust handling of some edge cases: zero, NaN, Inf.
* Fraction function accepts name-value or structure input arguments.

1.8.0.0

- Minor edit in blurb.

1.7.0.0

- Minor edit to FEX blurb.

1.6.0.0

* Add money/cheque number type.
* Allow options to be supplied as name-value pairs.

1.5.0.0

- I accidentally uploaded an older version. This version matches the online description :)

1.4.0.0

* comma control option.
* scale control option (short, long, Rowlett, Peletier).
* all names upto realmax.
* limit significant figures for floats.
* remove option <cardinal>.
* change option value 'ame' -> 'ae'.
* option fields case insensitive.

1.3.0.0

- Downloadable as toolbox

1.2.0.0

- Fix bug for ordinals 'first' and 'second'.
- Expand options table with more examples.

1.1.0.0

- Improve decimal fraction handling.
- Improve input checking.

1.0.0.0