Click here to Skip to main content
Full site     10M members (37.9K online)    

Converting numbers to the word equivalent.

Introduction

This is an alternative Tip for Converting numbers to the word equivalent. by OriginalGriff.

Using the Code

After looking at the code of OriginalGriff, I found a shorter alternative:

static string[] numbers = new string[] { 
    "Zero", 
    "One", 
    "Two", 
    "Three",
    ... ,
    "Five hundred and sixty-eight",
    "Five hundred and sixty-nine",
    ...
};
public static string ConvertToWords(int number)
{
    if (number < 0)
    {
        return "Negative numbers not supported";
    }
    else if (numbers.Length > number)
    {
        return numbers[number];
    }
    else
    {
        return "Number too large";
    }
}

Please note: you have to start with "Zero", otherwise the code don't will work. And if you end with "Thousand" for example, you have to add each number from 0 to 1000. If you forget "Five" for example, my code don't will work properly.

Advantages of my code: Disadvantages:
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
GeneralMy vote of 5
Matt T Heffron
9hrs 32mins ago 
GeneralMy vote of 5
keefe higgins
15hrs 39mins ago 
Question+5
Simon_Whale
22hrs 45mins ago 
AnswerRe: +5
ProgramFOX
14hrs 39mins ago 
QuestionDown votes compensated
OriginalGriff
23hrs 51mins ago 
GeneralMy vote of 1
Klaus Luedenscheidt
12 May '13 - 19:28 
GeneralMy vote of 1
vijayksingh
12 May '13 - 18:32 

Last Updated 12 May 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013