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

WBOY
發布: 2016-06-07 16:25:16
原創
1232 人瀏覽過

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



  
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!