
.NET 1.1+Convert A Number into Words
When developing commercial applications, particularly when those applications perform activities such as cheque printing, it can be necessary to display numeric values using the equivalent text. This article describes an algorithm to achieve this.
Algorithm Goals
There are many occasions when it is necessary to output a numeric value using text rather than numeric digits. A common example is the production of cheques. These usually require both the numeric and the spoken-word equivalent text to be printed. Unfortunately, the .NET framework does not provide a simple method by which this conversion can be made so a custom algorithm must be developed.
This article describes an algorithm that converts a numeric value into the equivalent text. It goes on to provide the C# code for this task. For simplicity, the article concentrates on the generation of English words for 32-bit integer values. However, you can extend the method easily to add other languages, decimal number conversion and currency information.
Large Number Names
The English names of the smaller numbers are generally accepted. However, once a number to be converted reaches a value of one thousand million or greater, the names of numbers vary according to usage. The algorithm described in this article uses the modern short scale numbering. In this scale, one thousand million is equivalent to one billion. This is different to the less-used traditional British long scale numbering where a billion is one million million.
The following table lists the short scale number names included in the algorithm and their numeric values. The range of numbers has been selected to provide for the full range of values that may be held in a 32-bit integer. If you wish to extend the functionality of the algorithm to larger numbers, a Wikipedia article exists that lists the names of large numbers.
| Value | Name |
|---|
| 1 | one |
| 10 | ten |
| 100 | hundred |
| 1,000 | thousand |
| 1,000,000 | million |
| 1,000,000,000 | billion |
Number Rules
The set of rules for converting an integer number to text initially appear to be reasonably complex. However, once analysed, they can be separated into a small number of distinct rule groups that are simpler to implement individually. To provide a single algorithm for all integer values there are six such rule groups:
- Zero Rule. If the value is zero then the number in words is 'zero' and no other rules apply.
- Three Digit Rule. The integer value is split into groups of three digits starting from the right-hand side. Each set of three digits is then processed individually as a number of hundreds, tens and units. Once converted to text, the three-digit groups are recombined with the addition of the relevant scale number (thousand, million, billion).
- Hundreds Rules. If the hundreds portion of a three-digit group is not zero, the number of hundreds is added as a word. If the three-digit group is exactly divisible by one hundred, the text 'hundred' is appended. If not, the text "hundred and" is appended. eg. 'two hundred' or 'one hundred and twelve'
- Tens Rules. If the tens section of a three-digit group is two or higher, the appropriate '-ty' word (twenty, thirty, etc.) is added to the text and followed by the name of the third digit (unless the third digit is a zero, which is ignored). If the tens and the units are both zero, no text is added. For any other value, the name of the one or two-digit number is added as a special case.
- Recombination Rules. When recombining the translated three-digit groups, each group except the last is followed by a large number name and a comma, unless the group is blank and therefore not included at all. One exception is when the final group does not include any hundreds and there is more than one non-blank group. In this case, the final comma is replaced with 'and'. eg. 'one billion, one million and twelve'.
- Negative Rule. Negative numbers are always preceded by the text 'negative'.
2 February 2007