Yah*_*hya 12 c# double datatable decimal formula
使用DataTable.Compute,并建立了一些案例进行测试:
dt.Compute("0/0", null); //returns NaN when converted to double
dt.Compute("0/0.00", null); //results in DivideByZero exception
Run Code Online (Sandbox Code Playgroud)
我已经更改了代码以同时处理两者。但是想知道这里发生了什么吗?
我猜想,发生这种情况是因为带有小数点的文字受到威胁System.Decimal并导致DivideByZeroException
根据MSDN
整数文字[+-]?[0-9] +被视为System.Int32,System.Int64或System.Double
不带科学计数法但带小数点的实际文字将被视为System.Decimal。如果该数字超过System.Decimal支持的最大值或最小值,则将其解析为System.Double。
试图将整数或十进制值除以零时抛出的异常。
因为System.Double它返回Nan,因为如果操作是除法并且常量是整数,则根据参考源,它会更改为双精度结果类型(感谢@ steve16351表示高兴)