Home > Database > Mysql Tutorial > body text

注意mysql 中一订要用decimal标识货币的值

WBOY
Release: 2016-06-07 16:25:16
Original
1232 people have browsed it

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

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



  
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!