TypeConverter
A class that handles the detection and conversion of certain resource formats / content types into other formats.
Table of Contents
Version: 1.5
Requires: PHP 5.2
Tested On: PHP 5.3
Commit Hash: 892b571411
Released: May 14th 2011, 23:11
Requires: PHP 5.2
Tested On: PHP 5.3
Commit Hash: 892b571411
Released: May 14th 2011, 23:11
Numword
Version: 1.5 (logs)
Package: Number to Word Converter
Category: PHP
Views: 7,638
Permalink -
Tinylink
A small script that can convert a number to its word equivalent. Can work with single numbers, multiple numbers, currency and within text blocks.
Numword is a play on words for: Number Word Converter.
Class Features:
Numword is a play on words for: Number Word Converter.
Class Features:
- Convert single or multiple numbers
- Convert USD currency
- Convert all numbers within a string
- Supports up to octillions
Top
1 - Converting Numbers
It's rather easy to convert numbers to a word. To do so you use the single() or multiple() methods. The single() method will accept 1 argument - which should be an integer - and will return its word equivalent. By default, all words are separated by a dash (-), but can be changed by editing the $sep property.
The multiple() method will take an array of integers as its first argument and will return an array with the respective words.
Notice! Really large numbers must be passed as strings or PHP will fail on 32bit integers.
echo Numword::single(1234); // one-thousand, two-hundred thirty-four
The multiple() method will take an array of integers as its first argument and will return an array with the respective words.
print_r(Numword::multiple(array(123, 5643, 64))); /* Array ( [0] => one-hundred twenty-three [1] => five-thousand, six-hundred forty-three [2] => sixty-four ) */
Notice! Really large numbers must be passed as strings or PHP will fail on 32bit integers.
Top
2 - Converting Currency
As of right now, the currency() method will take an amount and return it to its dollar word format. This only works for USD, but you could easily edit the currency() method and replace the $ and "dollars" with the currency text you wish.
The currency() method also takes a second argument to translate the words.
echo Numword::currency('$6934.34'); // six-thousand, nine-hundred thirty-four dollars and thirty-four cents
The currency() method also takes a second argument to translate the words.
echo Numword::currency('£6934.34', array('dollar' => 'pound(s)', 'cent' => 'pence')) // six-thousand, nine-hundred thirty-four pound(s) and thirty-four pence
Top
3 - Converting Numbers Within A String
If for some reason you have a number stuck right in the middle of a string and can't convert that number by itself, you would use the block() method. The block() method will convert all numbers that are found within the given string.
echo Numword::block('I am 21 years old, but my friend is 23.'); // I am twenty-one years old, but my friend is twenty-three.
Top
Read the whole documentation? Download the script now and try it yourself! Return to the Top
4 - Translating
Numword doesn't really have built in localization support (excluding currency()) but you can translate the output very easily. To do so, simply overwrite the static variables with your own since they are public. For example, in Spanish:
Numword::$digits = array('cero', 'uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve');