【发布时间】:2016-12-03 09:22:07
【问题描述】:
谁能帮我解决这个问题?
我在数据库中有一个可以为空的“真实”列,实体框架表示为“浮点数?”
系统会以十进制进行一些计算,但是在分配此列中的值时会丢失精度。
系统正在使用下一次转换:
dbObject.floatCol = (float) decimalVar;
decimalVar = 123.12341
但是 dbObject.floatCol 是 = 123.1234
我一直在尝试以其他方式进行这种转换,但没有运气。
注意事项:
- 在数据库中,我可以毫无问题地将值更新为 123.12341。
- 我无法更改数据库的列
- 我正在使用带有 .NET Framework 4 的 Visual Studio 2010
编辑:
如果我调试问题(使用问题的真实数字),我有:
decimal decimalVar = new decimal(123.13561);
// decimalVar is 123.13561
float? floatVar = 0;
// floatVar is 0
floatVar = (float)decimalVar;
// floatVar is 123.135612 <- has appeared a 2 at the end
// with a different decimal number
decimalVar = new decimal(128.13561); // replaced the 123 for 128
// decimalVar is 128.13561
floatVar = (float)decimalVar;
// floatVar is 128.1356 <- has disappeared the 1 at the end
这种行为真的很奇怪
我需要知道是否可以进行正确的转换
当我尝试将值打印到控制台或日志文件时,它们是不同的:s
【问题讨论】:
-
C#和SQLServer的类型映射可以找到here
标签: c# .net type-conversion decimal