包含数字的字符可使用 char.GetNumericValue 方法转换为数字(双精度浮点型)。spa
代码以下:code
double d = char.GetNumericValue('5'); Console.WriteLine(d);
输出值:5htm
那么,为何当一个 char ,而且只能为单字符时,GetNumericValue 会返回一个单位数“数字”(“0”到“9”)的 double 变量呢?blog
这是由于 char 保存 Unicode 字符,它能够容纳三分之二的 Unicode 字符。当这三分之二的 Unicode 字符与 GetNumericValue 方法一块儿使用时,返回值为:0.666666666666667。ip
章节:Converting Chars to Doubles
译书名:《C# 奇淫巧技 -- 编写更优雅的 C#》
原书名:《C# Tips -- Write Better C#》
网址:https://leanpub.com/cstipsget