我有一个带一些值的浮点型变量,有一个点,这个值被转换成一个双精度类型的变量,这时,浮点型变量中的值不再是我在双精度类型变量中看到的。为了使这个示例易于理解,我在即时窗口中执行此操作,以生成正在发生的内容:
(double)float.Parse ( "-0.00146256")给我-0.001462560030631721
"-0.00146256“(在字符串yes中)是这个值的来源,它存储在一个浮点型中,在代码中的某个位置使用float.Parse(),然后在稍后转换为double。
为什么双精度变量中的值更改为不同的值,我可以做些什么来防止这种不精确的行为?
发布于 2015-01-30 20:14:16
这就是它应该如何工作的。微软称
This type is useful for applications that need large numbers but do not need precise accuracy. If you require very accurate numbers, consider using the Decimal data type.
您可以先转换为小数,然后转换为双精度:
double d = (double)(decimal)float.Parse("-0.00146256");https://stackoverflow.com/questions/28235638
复制相似问题