3

I wan't to convert a int to a char, but I don't want the (char)int method, because (char)3 won't give '3' but ''.

So, there's a built-in way to get what I want, or I need to make it myself?

3 Answers3

8

I think this is the easiest, most readable way:

(char)('0' + 3)

If you want to be more culture aware, you can use this.

CultureInfo.CurrentCulture.NumberFormat.NativeDigits[3]
recursive
  • 83,943
  • 34
  • 151
  • 241
1
(char)('0' + IntegerValue)

Note: you use 3 so your integer value is 3 you can write 3 instead of integer value

zafar hayat
  • 98
  • 1
  • 8
0

Just as an alternative, from earlier comments (with special thanks to @recursive):

int yourInt = 3; //can be double or decimal as well
char val = yourInt.ToString()[0];

Although it takes the first digit, it will work for other numeric types as well (double, decimal, ...).

Stefan
  • 17,448
  • 11
  • 60
  • 79