Click here to Skip to main content
13,731,434 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.9K views
2 bookmarked
Posted 12 May 2013
Licenced CPOL

Converting numbers to the word equivalent.

, 12 May 2013
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

I'm a hobbyist programmer. I work mostly in C#, JavaScript, and Python. I also like chess; you can find me on Lichess.

You may also be interested in...

Comments and Discussions

 
QuestionMy (late) vote of 5 Pin
CHill6022-Oct-14 0:10
protectorCHill6022-Oct-14 0:10 
GeneralRe: My (late) vote of 5 Pin
Thomas D [ProgramFOX]22-Oct-14 2:16
mvpThomas D [ProgramFOX]22-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
memberkeefe higgins13-May-13 5:40 
Question+5 Pin
Simon_Whale12-May-13 22:34
professionalSimon_Whale12-May-13 22:34 
AnswerRe: +5 Pin
Thomas D [ProgramFOX]13-May-13 6:40
memberThomas D [ProgramFOX]13-May-13 6:40 
QuestionDown votes compensated Pin
OriginalGriff12-May-13 21:28
protectorOriginalGriff12-May-13 21:28 
GeneralMy vote of 1 Pin
Klaus Luedenscheidt12-May-13 19:28
memberKlaus Luedenscheidt12-May-13 19:28 
GeneralMy vote of 1 Pin
Singh Vijay Kumar12-May-13 18:32
memberSingh 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 12 May 2013
Article Copyright 2013 by Thomas D [ProgramFOX]
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid