为什么在 C# 中将 double 转换为 float 时我的精度会下降?
Why my precision getting loss while converting double to float in c#?
我在 c# 中具有双倍值 10293.01416015625,我正在尝试转换为浮点数。由于 float 只有 24 位,我想得到的结果是 10293.0141。但我得到的价值是 10293.0137
double value = 10293.01416015625;
float converted = (float)value;
预期值 - 10293.0141
正在获取值 - 10293.0137
提前致谢
来自System.Single documentation:
A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.
您的结果最多 7 位有效数字 (10293.01) 是正确的。您不应该期望 float
.
可以获得更多
最接近 10293.01416015625 的 float
的确切值为 10293.013671875 和 10293.0146484375。两者都与您要表示的值相差 0.00048828125。
我在 c# 中具有双倍值 10293.01416015625,我正在尝试转换为浮点数。由于 float 只有 24 位,我想得到的结果是 10293.0141。但我得到的价值是 10293.0137
double value = 10293.01416015625;
float converted = (float)value;
预期值 - 10293.0141
正在获取值 - 10293.0137
提前致谢
来自System.Single documentation:
A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.
您的结果最多 7 位有效数字 (10293.01) 是正确的。您不应该期望 float
.
最接近 10293.01416015625 的 float
的确切值为 10293.013671875 和 10293.0146484375。两者都与您要表示的值相差 0.00048828125。