Search code examples
c#unicodechargethashcode

Will converting Char.GetNumericValue() to int destroy information?


I am implementing GetHashCode(). I want to sum the values in a property called string Id then divide by some constant and return the result. I am using GetNumericValue():

int sum = 0;
 foreach (var ch in Id)
  sum += char.GetNumericValue(ch);

But it seems that GetNumericValue returns double. is it ok to convert it into an int?

I thought that Unicode characters are represented by whole number, why is double returned? And is it okay to ignore it?


Solution

  • Why is double returned?

    While 0-9 are the standard digits, actually there are some Unicode characters that represent numbers, some of them are floating point like or ½. Let's get an example:

    var ch = char.GetNumericValue('½');
    Console.WriteLine(ch);// 0.5 output