日期:2014-05-16  浏览次数:20743 次

注意mysql 中一定要用decimal标识货币的值
注意mysql 中一定要用decimal标识货币的值!不要用float了,举例说明:
  
Create Table LedgerEntries
(
LedgerEntryID Int Primary Key Auto_Increment Not Null
,CustomerID Int Not Null
,Amount Float Not Null
);

然后插入一些数据;
Insert Into LedgerEntries (CustomerID, Amount)
Values (1, 3.14);

Insert Into LedgerEntries (CustomerID, Amount)
Values (1, 30000.14);

最后查询下
Select * From LedgerEntries;

+---------------+------------+---------+
| LedgerEntryID | CustomerID | Amount  |
+---------------+------------+---------+
|             1 |          1 |    3.14 |
|             2 |          1 | 30000.1 |
+---------------+------------+---------+



看到了么?没了最后的一位!,因此,赶紧用decimal吧

  Create Table LedgerEntries
(
LedgerEntryID Int Primary Key Auto_Increment Not Null
,CustomerID Int Not Null
,Amount Decimal(10,2) Not Null
);

Insert Into LedgerEntries (CustomerID, Amount)
Values (1, 3.14);

-- This is the largest value we can insert into a Decimal(10,2)
-- if we have two numbers to the right of the decimal point
Insert Into LedgerEntries (CustomerID, Amount)
Values (1, 99999999.99);

Select * From LedgerEntries;
+---------------+------------+-------------+
| LedgerEntryID | CustomerID | Amount      |
+---------------+------------+-------------+
|             1 |          1 |        3.14 |
|             2 |          1 | 99999999.99 |
+---------------+------------+-------------+
2 rows in set (0.00 sec)