What is the best field definition to store a .NET decimal into MySQL?

蹲街弑〆低调 提交于 2020-01-04 06:21:13

问题


I need to store decimals into MySQL, which can have a varying precision. Therefore I would be interested to know which MySQL field type is absolutely equivalent to .NET's decimal structure, if any.

I plan to use Dapper as a lightweight ORM.


回答1:


The .net decimal can be different datatypes under the hood.

.net formats                                  MySQL
----------------------------------------------------
Decimal(Double)                              Float
Decimal(Int32)                               DECIMAL
Decimal(Int32())                             DECIMAL
Decimal(Int64)                               DECIMAL
Decimal(Single)                              DECIMAL
Decimal(UInt32)                              DECIMAL
Decimal(UInt64)                              DECIMAL
Decimal(Int32, Int32, Int32, Boolean, Byte)  DECIMAL
//This is really a UINT96.  

Warning
Note that according to Jon Skeet, decimal can be declared in lots of ways, but will always be a FLOAT under the hood, with all the rounding errors that brings, you have been warned.
See: SQL decimal equivalent in .NET

MySQL's DECIMAL takes up more space if you assign it a larger precision.

From the manual: http://dev.mysql.com/doc/refman/5.5/en/precision-math-decimal-changes.html

Values for DECIMAL columns in MySQL 5.5 are stored using a binary format that packs nine decimal digits into 4 bytes.

The largest number of digits is 65, divided by 9 = 8 bytes, an INT128.



来源:https://stackoverflow.com/questions/7348593/what-is-the-best-field-definition-to-store-a-net-decimal-into-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!