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?
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
Yes, it will lose data for some values , since this is the numerical value of unicode characters that are themselves numbers - and unicode includes characters that are non-integer numbers:
var val = char.GetNumericValue('½'); // or ¼, or ꠲, or ⳽
System.Console.WriteLine(val);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.