最好的字段定义来存储一个.NET十进制数转换成MySQL的是什么?的是、最好的、字段、转换成

2023-09-06 19:12:59 作者:趴趴の熋★

我需要小数存储到MySQL,它可以有一个不同的precision。因此,我很想知道MySQL的字段类型是绝对等同于.NET的十进制结构,如果有的话。

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.

我打算使用小巧玲珑作为一个轻量级的ORM。

I plan to use Dapper as a lightweight ORM.

推荐答案

在.NET小数可以引擎盖下不同的数据类型。

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.  

警告 需要注意的是,根据乔恩斯基特,十进制可以在很多方面被宣布,但将永远是一个 FLOAT 下油烟机,所有的舍入误差带来,你已经被警告。 请参阅: SQL十进制等效于.NET

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

如果您分配一个较大的precision MySQL的十进制占用了更多的空间。

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

这本手册: HTTP ://dev.mysql.com/doc/refman/5.5/en/$p$pcision-math-decimal-changes.html

值在MySQL 5.5 DECIMAL列使用的是包9个十进制数字为4个字节的二进制格式存储。

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

最大位数为65,9 = 8个字节,一个INT128分。

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

 
精彩推荐
图片推荐