Currently working on a forum site which surely has numbers in various places representing different things. Being a slightly tidy guy https://www.codeproject.com/script/Forums/Images/smiley_wink.gif, I strive to make the numbers look trimmed by formatting them with number_format ("putting the integers into comma"). But taking a step further, I decided to add a mouse-over effect to spell out the numbers (i.e., when you point on "12, 345 user online" the title attribute reads "Twelve Thousand, Three Hundred and Forty Five users online"). What I came up with can convert
any positive integer up to (1x1064)-1 any number from -((1x1064)-1) to (1x1064)-1 to it word equivalent though the number has to be entered as a
string if it is greater than
PHP_INT_MAX to prevent PHP from exponenting it (i.e. changing 2000 to 2e3).
The Process (using -1234.56 as a case study)
Based on the knowledge or arithmetic from elementary school, all it takes to convert numbers to their word equivalent is:
If number is negative (as - in front)
Note that it is negative and remove the - sign
this leaves us with 1234.56
Check for position of .
Remove every thing after . and focus on the remain part first
Divide the numbers into group of three from the right.
1234 => 1/234
On each group, exponent each character with 0, 1, and 2 starting from the right-most
1/234 => 10
Starting from the right-most, convert its element to it word equivalent with exponent 0 for unit, 1 for tens, and 2 for hundred.
=> Two hundred and thirty four
if there is the special case of 11 20 (the tens been a 1), combine the tens and the unit together.
This continues until the last group on the left.
Based on the position of each group-element in the group, the appropriate notation can then be appended to each group (the right-most has no notation, the second from right is thousand, the next million, next billion, etc.)
So, 234 has no notation; 1 is thousand;
1234 => One thousand, Two hundred and thirty four
Pick the right side of the . now and name then one at a time
so, 56 becomes Five Six
So all in all -1234.56 becomes
Negative One thousand, Two hundred and thirty four dot five six
The following is the algorithm:
- Cast the inputted number into a
- Check the string if it begins with -. If true note it for further time and remove the - sign
- Create a new array to hold the word equivalent.
- Check the position of . sign. If present get the digit after the . sign and remove them from the original input and proceed to Step 5 else go to Step 6.
- Split the newly gotten string into an array, reverse the array and push the individial word representation into the Step 3 Array, then push the word "dot" into the array.
- Split the "original"
string left in Step 4 into an array of character.
- Reverse the Step 6 array.
- Implode (join) the array into a
- Split the Step 8
string into an array of 3 character-per-element.
- From the step 9 array, take the first element (which will be a
string) and split it into an array.
- Check if the first element of the Step 10 array is
true, combine element zeroth element and first element together on and set zeroth element to
- Push word equivalent of zeroth and first element of Step 11 array (modified Step 10 array) to the Step 3 array.
- Check if second element of Step 11 array is not null or
true, check if neither of zeroth and first element is
true, push "and" to Step 3 array, then push "hundred" to the Step 3 array.
- Push the word equivalent of the second element of Step 11 array to Step 3 array.
- Push a comma ", " to Step 3 array.
- Repeat Step 10 to Step 15 until the last element of Step 9 array is reached.
- Pop the last element in the Step 6 array (to remove the last comma).
- If Step 2 was true, push "Negative" to the array
- Implode (join) Step 3 array into a
- Explode (split) the Step 19
string based on the space character.
- Reverse the Step 20 array.
- Implode (join) the Step 22 array into a space separated
Get the PHP source file attached to this article, place it in the same directory as your source file and use the function like this:
$num_word = new ext_functions;
echo $num_word ->word(12345); ?>
- As earlier stated, though the function accept integers, is it advised to input the parameter in
strings to avoid exponenting of the digits:
echo $num_word ->word("12345");
- There is no error checking to ensure that the inputted parameter is a valid positive integer, it is left to the user to ensure that (though this and negative integers will be included in future versions).
5 June 2013
The bug that makes 1000 appear as (One thousand,
) and 1000000 as (One million, thousand
) was fixed
3 May 2013
Negative numbers and decimal support was added.