简体   繁体   English

C#中的小数到浮点转换问题

[英]Decimal to Float conversion issue in C#

Can anyone please help me with this problem? 谁能帮我解决这个问题?

I have in the database a nullable "real" column that Entity Framework represents as "float?" 我在数据库中有一个可空的“真实”列,实体框架将其表示为“浮动”?

The system does some calculations in decimal but when assigns the value in this column the precision is lost. 系统以十进制进行一些计算,但是当在此列中分配值时,精度会丢失。

The system is using the next conversion: 系统正在使用下一次转换:

dbObject.floatCol = (float) decimalVar;

decimalVar is = 123.12341 decimalVar为= 123.12341

but dbObject.floatCol is = 123.1234 但是dbObject.floatCol是= 123.1234

I been trying to do this convertion in other ways without luck. 我一直在尝试以其他方式进行这种转换,但是没有运气。

NOTES: 笔记:

  1. In the database I can update the value to 123.12341 without problem. 在数据库中,我可以毫无问题地将值更新为123.12341。
  2. I can't alter the database's column 我无法更改数据库的列
  3. I'm using Visual Studio 2010 with .NET Framework 4 我正在将Visual Studio 2010与.NET Framework 4一起使用

EDIT: 编辑:
If I debug the problem (with the real numbers of my issue) I have: 如果我调试问题(使用问题的真实编号),我将:

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

The behavior is really weird 这种行为真的很奇怪
I need to know if is possible to do a correct conversion 我需要知道是否可以进行正确的转换
When I try to print the values to Console or Logfile they are different :s 当我尝试将值打印到控制台或日志文件时,它们是不同的:s

You are getting about 7 digits precision. 您将获得7位数的精度。 This is what you can expect from a C# float . 这就是C# float所能提供的。 If you need more precision, use a C# double value instead. 如果需要更高的精度,请改用C# double值。 It gives you about 15 to 16 digits precision. 它给您约15到16位数字的精度。

double d = (double)decimalVar;

An also you must make a difference between the C# data types and the database data types. 还必须在C#数据类型和数据库数据类型之间进行区别。 In C#, the best data type for reading in a db decimal column is decimal . 在C#中,要在db decimal列中读取的最佳数据类型是decimal

Depending on the database type, different names are used for the numeric column types. 根据数据库类型,数字列类型将使用不同的名称。 And those names are different from the C# types. 这些名称与C#类型不同。 A SQL-Server float corresponds to a C# double . SQL Server float对应于C# double A SQL-Server real corresponds to a C# float (.NET Single ). SQL Server real对应于C# float (.NET Single )。

See: SQL Server Data Type Mappings 请参阅: SQL Server数据类型映射

Concerning your EDIT: float and double values have a binary representation as a floating point number internally which requires decimal numbers to be rounded to the closest possible matching binary value. 关于您的EDIT: floatdouble值在内部具有二进制表示为浮点数,它要求将十进制数舍入为最接近的可能匹配二进制值。 This binary value is then converted back to a decimal representation for display. 然后将该二进制值转换回十进制表示形式进行显示。 Depending on whether the value was rounded up or down this might add or remove decimals. 根据值是向上舍入还是向下舍入,这可能会添加或删除小数。 This is what you experienced. 这就是你所经历的。

See Jon Skeet's article on Binary floating point and .NET 请参阅Jon Skeet关于二进制浮点和.NET的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM