11,704,315 members (57,686 online)
Tip/Trick

# Converting Numbers into their Words Equivalent

, 5 Jun 2013 CPOL 9.8K 156 10
 Rate this:
How to convert numbers [-((1x10^64)-1) up to (1x10^64)-1] to their word equivalent (i.e. -123.45 becomes Negative One Hundred And Twenty Three Dot Four Five)

## Introduction

Currently working on a forum site which surely has numbers in various places representing different things. Being a slightly tidy guy , 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 /22 31 40

Starting from the right-most, convert its element to it word equivalent with exponent 0 for unit, 1 for tens, and 2 for hundred.

22 31 40  => 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.

e.g. 11 20 => Twelve

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 Algorithm

The following is the algorithm:

1. Cast the inputted number into a `string`.
2. Check the string if it begins with -. If true note it for further time and remove the - sign
3. Create a new array to hold the word equivalent.
4. 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.
5. 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.
6. Split the "original" `string `left in Step 4 into an array of character.
7. Reverse the Step 6 array.
8. Implode (join) the array into a `string`.
9. Split the Step 8 `string `into an array of 3 character-per-element.
10. From the step 9 array, take the first element (which will be a `string`) and split it into an array.
11. Check if the first element of the Step 10 array is `1`, if `true`, combine element zeroth element and first element together on and set zeroth element to `0`.
12. Push word equivalent of zeroth and first element of Step 11 array (modified Step 10 array) to the Step 3 array.
13. Check if second element of Step 11 array is not null or `0`, if `true`, check if neither of zeroth and first element is `0`, if `true`, push "and" to Step 3 array, then push "hundred" to the Step 3 array.
14. Push the word equivalent of the second element of Step 11 array to Step 3 array.
15. Push a comma ", " to Step 3 array.
16. Repeat Step 10 to Step 15 until the last element of Step 9 array is reached.
17. Pop the last element in the Step 6 array (to remove the last comma).
18. If Step 2 was true, push "Negative" to the array
19. Implode (join) Step 3 array into a `string`.
20. Explode (split) the Step 19 `string `based on the space character.
21. Reverse the Step 20 array.
22. Implode (join) the Step 22 array into a space separated `string`.

## Usage Example

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:

```<?php
include "numtoword.php";
\$num_word = new ext_functions;
echo \$num_word ->word(12345); // or echo \$num_word ->word("12345");
?>```

## P.S.

1. As earlier stated, though the function accept integers, is it advised to input the parameter in `string`s to avoid exponenting of the digits:
`echo \$num_word ->word("12345"); `
2. 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).

## Update:

### 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.

## About the Author

 Software Developer Nigeria
A graduate of Agricultural Engineering from Ladoke Akintola University of Technology, Ogbomoso but computer and web programming is his first love. You can meet him on Facebook Osofem Inc.

## Comments and Discussions

 First Prev Next
 Good job. GeekBond 3-May-13 4:38 GeekBond 3-May-13 4:38
 This is not an article. OriginalGriff3-May-13 0:51 OriginalGriff 3-May-13 0:51
 Re: This is not an article. Oso Oluwafemi Ebenezer3-May-13 2:30 Oso Oluwafemi Ebenezer 3-May-13 2:30
 Re: This is not an article. GeekBond 3-May-13 4:42 GeekBond 3-May-13 4:42
 Last Visit: 31-Dec-99 18:00     Last Update: 28-Aug-15 13:29 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.