Click here to Skip to main content
Click here to Skip to main content

Tagged as

Converting Numbers into their Words Equivalent

, 5 Jun 2013
Rate this:
Please Sign up or sign in to vote.
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 Wink | <img src=, 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 strings 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Oso Oluwafemi Ebenezer
Software Developer
Nigeria 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.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralGood job. Pinmember GeekBond 3-May-13 4:38 
QuestionThis is not an article. PinmvpOriginalGriff3-May-13 0:51 
AnswerRe: This is not an article. PinmemberOso Oluwafemi Ebenezer3-May-13 2:30 
AnswerRe: This is not an article. Pinmember GeekBond 3-May-13 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 5 Jun 2013
Article Copyright 2013 by Oso Oluwafemi Ebenezer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid