Convert.ToDecimal(float number here)
调用时出现溢出异常。我正在读取字节字符串并希望将字节数组转换为十进制数。
byte[] hexbyte = {45, 6, 7, 29};
float myFloat = BitConverter.ToSingle(hexbyte, 0);
//Make sure the float can be converted to decimal
if (!float.IsNaN(myFloat) ||
!float.IsInfinity(myFloat) ||
myFloat < (float)decimal.MaxValue ||
myFloat > (float)decimal.MinValue)
{
try {
myFloatD = Convert.ToDecimal(myFloat);
}
catch (Exception ex)
{
Debugger.Log(0, "1", ex.ToString());
return 0;
}
}
else
return 0;
myFloat = (float)Math.Round(myFloatD, 2); // 1 indicates the decimal places
即使我设置约束来检查浮点数是否太大/太小/到无穷大以进行转换,代码仍然会以捕获异常结束...