Is there a way in .Net to get a string value for an int word?

For example:

(1).SomeFunction().Equals("one") (2).SomeFunction().Equals("two") 

I really need this only for numbers 1-9 in the case when I work, should I just use the switch / select event?

Update. In this case, I do not need localization.

Update 2 Here is what I ended up using:

 Private Enum EnglishDigit As Integer zero one two three four five six seven eight nine End Enum (CType(someIntThatIsLessThanTen, EnglishDigit)).ToString() 
+7
string integer int
source share
7 answers

How about the listing?

 enum Number { One = 1, // default value for first enum element is 0, so we set = 1 here Two, Three, Four, Five, Six, Seven, Eight, Nine, } 

Then you can enter things like ...

 ((Number)1).ToString() 

If you need localization, you can add a DescriptionAttribute to each enumeration value. The property of the Description attribute will retain the name of the resource key.

 enum Number { [Description("NumberName_1")] One = 1, // default value for first enum element is 0, so we set = 1 here [Description("NumberName_2")] Two, // and so on... } 

The following function will capture the value of the Description property from the attribute

 public static string GetDescription(object value) { DescriptionAttribute[] attributes = null; System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString()); if (fi != null) { attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); } string description = null; if ((attributes != null) && (attributes.Length > 0)) { description = attributes[0].Description; } return description; } 

This can be called as follows:

 GetDescription(((Number)1)) 

From this, you can pull the appropriate value from the resource file or simply call .ToString() if null was returned.

Edit

Various commentators have indicated (and I must agree) that it would be easier to just use the names of the enumeration values ​​to refer to localized strings.

+11
source share

create a dictionary of strings:

 string[] digits = new string[] { "zero", "one", "two", ... }; string word = digits[digit]; 
+4
source share

Use a lookup table; an array will do. This is no slower than an enumeration, and it is easier to localize.

change

Andrey's code example is what I suggested, although I think it is called a dictionary, a bit confusing.

+1
source share

If you don't need localization, I would suggest Richard Eve's solution. However, for localization, I would suggest adding ten-digit names to the resource file, for example, NumberName_0 - NumberName_9 . Thus, when searching for a number, you can simply load the resource with the name String.Format("NumberName_{0}", mydigit) .

The same method, by the way, is also great for localized enumeration or description names.

+1
source share

Why stop at 1-9 ...

The C # version of the Squeak Smalltalk method does this for all vigintillion numbers:

  public static String AsWords(this int aNumber) { var answer = ""; if (aNumber == 0) return "zero"; if (aNumber < 0) { answer = "negative"; aNumber = Math.Abs(aNumber); } var thousands = new[] {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion","octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion"}; var thousandCount = 0; while (aNumber > 0) { var underOneThousandName = ThreeDigitName(aNumber % 1000); aNumber = aNumber / 1000; if(underOneThousandName != "") { if (answer != "") answer = "," + answer; answer = underOneThousandName + " " + thousands[thousandCount] + answer; } thousandCount += 1; } return answer; } private static string ThreeDigitName(int aNumberLessThanOneThousand) { if (aNumberLessThanOneThousand == 0) return ""; var units = new[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen"}; var answer = ""; if (aNumberLessThanOneThousand > 99) { answer = units[(aNumberLessThanOneThousand / 100) - 1] + " hundred"; if (aNumberLessThanOneThousand % 100 != 0) answer += " " + ThreeDigitName(aNumberLessThanOneThousand % 100); return answer; } if (aNumberLessThanOneThousand < 20) return units[aNumberLessThanOneThousand -1]; var multiplesOfTen = new[] {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; answer += multiplesOfTen[(aNumberLessThanOneThousand / 10)-2]; if (aNumberLessThanOneThousand % 10 != 0) answer += "-" + units[(aNumberLessThanOneThousand % 10)-1]; return answer; } 
+1
source share

I do not think that there are built-in functions for this. I would use a case of choice.

0
source share

If you are using 2008:

 public static String AsWord(this int aNumber) { return ((Number) aNumber).ToString(); } enum Number { One = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, } 
0
source share

All Articles