Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Converting numbers to the word equivalent.

Rate me:
Please Sign up or sign in to vote.
4.41/5 (8 votes)
12 May 2013CPOL 19.7K   2   9
This is an alternative for "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:

C#
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)


Written By
Student
Europe Europe
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy (late) vote of 5 Pin
CHill6022-Oct-14 0:10
mveCHill6022-Oct-14 0:10 
GeneralRe: My (late) vote of 5 Pin
Thomas Daniels22-Oct-14 2:16
mentorThomas Daniels22-Oct-14 2:16 
GeneralMy vote of 5 Pin
Matt T Heffron13-May-13 11:47
professionalMatt T Heffron13-May-13 11:47 
GeneralMy vote of 5 Pin
keefe higgins13-May-13 5:40
keefe higgins13-May-13 5:40 
Question+5 Pin
Simon_Whale12-May-13 22:34
Simon_Whale12-May-13 22:34 
AnswerRe: +5 Pin
Thomas Daniels13-May-13 6:40
mentorThomas Daniels13-May-13 6:40 
QuestionDown votes compensated Pin
OriginalGriff12-May-13 21:28
mveOriginalGriff12-May-13 21:28 
GeneralMy vote of 1 Pin
Klaus Luedenscheidt12-May-13 19:28
Klaus Luedenscheidt12-May-13 19:28 
GeneralMy vote of 1 Pin
Singh Vijay Kumar12-May-13 18:32
professionalSingh Vijay Kumar12-May-13 18:32 

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

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