4

Using C# I need to convert an integer to it's word representation, and only go up to 5 to 10. Is there a better way to do it than this?

static string[] numberwords =
{
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five"      
};

public static string GetNumberAsString(int value)
{
    return numberwords[value];      
}
clickatwill
  • 950
  • 1
  • 10
  • 16
  • 1
    possible duplicate of [Code Golf: Number to Words](http://stackoverflow.com/questions/309884/code-golf-number-to-words) – LBushkin Oct 20 '10 at 16:21
  • 2
    @LBushkine: But we should not close a real question as dupe of a code-golf. The OP does not ask for ultra short mutilated code. – H H Oct 20 '10 at 16:27
  • Edited to mention language. I was just asking in case there was a better way (read: more efficient) to do it rather than using the array, not write a ground-breaking algorithm. – clickatwill Oct 20 '10 at 16:36
  • and people mark this question with +1? – smirkingman Oct 20 '10 at 19:56

5 Answers5

1

How far up do you need to go?

My guess is that you'd need several arrays to do what you need for the general case, like "one" through "twenty", "twenty" through "ninety" by tens, "thousand", "million", etc.

John
  • 15,990
  • 10
  • 70
  • 110
  • this is the best option!! then have logic to string the words together based on the value – jimplode Oct 20 '10 at 16:34
  • I was delivered some CSS and Html that has these numbers as classes. I've yet to see anything over "five" – clickatwill Oct 20 '10 at 16:38
  • @Will, in that case, the only way to make it easier is to create a type extender for Int32 that allows you to get at the name. (see my answer for example). – Brad Oct 20 '10 at 16:42
1

Why don't you use an associative array (Dictionary<int,string> in C#)? If you use that, you wouldn't need to worry about the order of strings. Because it would become a nightmare as you add more numbers and if they are not necessarily continuous. Of course, if you are range of numbers is very small, your approach would save you some space.

rkg
  • 5,559
  • 8
  • 37
  • 50
1

For this specific case (limited range of numbers), an interesting alternative would be to use an enum, and then wrap the conversion to string in an extension method.

The code below shows converting from an enum value to string, using the extension method, and trying to convert a value outside the range of the enum.

public enum NumberWords
{
    zero,
    one,
    two,
    three,
    four,
    five
}
public static class IntExtensions
{
    public static string ToWord(this int input)
    {
        return ((NumberWords)input).ToString();
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(((NumberWords)5).ToString());

        Console.WriteLine(0.ToWord());
        Console.WriteLine(1.ToWord());
        Console.WriteLine(2.ToWord());
        Console.WriteLine(123.ToWord());

        Console.ReadLine();
    }
}
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
0

Short:

No.

Long:

The .NET framework provides no way to do it. One suggestion for improvement: You could read the the numbers from a language dependent configuration file.

codymanix
  • 28,510
  • 21
  • 92
  • 151
0

To piggy-back on @Ravi's suggestion, you could lookup each digit by place (ones, tens, hundreds, etc) in a custom dictionary like Dictionary<DigitPlace, Dictionary<int, string> (where you create and Enum for DigitPlace). Of course this would be language specific.

Sample:

Ones, 1, "one"
Ones, 2, "two"
...
Tens, 3, "thirty-"
...
Hundreds, 6, "six-hundred"
...

If you want to follow my suggestion to create type extender, here is a sample.

public enum NumberedWords
{
    zero = 0,
    one = 1,
    two = 2,
    three = 3,
    four = 4,
    five = 5
}

public static string ToName(this int value)
{
    return (NumberedWords)value;
}

Use:

int number = 5;
string numberName = number.ToName();
Brad
  • 15,361
  • 6
  • 36
  • 57