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

Converting numbers to the word equivalent.

By , 12 May 2013
 

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:
  • Shorter (If you add three values to the array, my code isn't shorter. However, if you add 100 values to the array, my code is shorter than the code of OriginalGriff)
  • Faster at runtime
Disadvantages:
  • It costs more memory

License

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

About the Author

ProgramFOX
Belgium Belgium
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
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 this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5professionalMatt T Heffron2hrs 28mins ago 
GeneralMy vote of 5memberkeefe higgins8hrs 35mins ago 
Question+5professionalSimon_Whale15hrs 41mins ago 
AnswerRe: +5member ProgramFOX7hrs 35mins ago 
QuestionDown votes compensatedprotectorOriginalGriff16hrs 47mins ago 
GeneralMy vote of 1memberKlaus Luedenscheidt18hrs 47mins ago 
GeneralMy vote of 1membervijayksingh19hrs 43mins ago 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 12 May 2013
Article Copyright 2013 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid