注意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)