decimal 详解

2018-04-27  本文已影响0人  __XY__

本篇文章回答了如下问题:
1.为什么要有decimal,解决了什么问题,为什么不用float
2.decimal为什么就不存在不精确的问题
3.mysql中有对应的字段吗
4.mysql中有对应的字段吗
5.python中decimal如何来使用
6.python中decimal如何用特定格式展示

1.为什么要有decimal,解决了什么问题,为什么不用float

2.decimal为什么就不存在不精确的问题?

3.mysql中有对应的字段吗

试着创建一个表

create table t1 (a_decimal decimal(4,2));
insert into t1 set a_decimal = 100;

会出现如下报错
1264, u"Out of range value for column 'a_decimal' at row 1"
继续
insert into t1 set a_decimal = 10;
显示插入成功;

mysql dbu@(none):test> select * from t1;

+-----------+

| a_decimal |

+-----------+

| 10.00  |

| 99.00  |

+-----------+

insert into t1 set a_decimal = 10.2392323;

insert into t1 set a_decimal = 10.2312323;


+-----------+

| a_decimal |

+-----------+

| 10.00  |

| 99.00  |

| 10.00  |

| 10.23  |

| 10.24  |

+-----------+

4.使用场景有哪些

5.python中decimal如何来使用

Decimal ( 3.14 )
Decimal('3.140000000000000124344978758017532527446746826171875')

运行上面命令可以看出来decimal和float的区别

6.python中的decimal如何用特定格式展示

{0:.2f}.fromat(Decimal('32.2222'))
上一篇 下一篇

猜你喜欢

热点阅读