开发者

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

开发者 https://www.devze.com 2023-04-03 07:49 出处:网络
I need to store decimals into MySQL, which can have a varying precision. Therefore I would be interested to know which MySQL f开发者_开发知识库ield type is absolutely equivalent to .NET\'s decimal str

I need to store decimals into MySQL, which can have a varying precision. Therefore I would be interested to know which MySQL f开发者_开发知识库ield type is absolutely equivalent to .NET's decimal structure, if any.

I plan to use Dapper as a lightweight ORM.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号