美文网首页大数据 爬虫Python AI SqlPython小哥哥
万能的Python竟然算不明白“钱”?损失惨重!

万能的Python竟然算不明白“钱”?损失惨重!

作者: 14e61d025165 | 来源:发表于2019-05-20 17:00 被阅读0次

金融行业的同学,经常会遇到“钱”的转换问题,在进行金额的计算和转化时,稍微有不慎,则会损失惨重。

1、带来经济损失,可能比较微小,

2、严重影响了后续的日终对账业务,

3、客户的投诉也会比较严重,严重影响公司的形象。

因而,在使用Python的时候,需要学会算明白“钱”。

先看一个例子,由“元”转换为“分”, Python代码如下:

a = 66.9 >>> a * 100 6690.000000000001

咦,是不是令你大吃一惊?这么简单的运算Python竟然会有误?

确实转换为分之后竟然少了0.000000000001。这种微小的差别对于日终对账是致命的,可能运维人员需要排查一整晚,为了这小小的0.000000000001。有人可能说,这个问题简单,直接取整就可以了。代码如下:

round(a * 100) 6690

事情真没有这么简单,金融计算对精度的要求不是一般高,四舍五入的操作同样会导致账目不平。

Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

那么,如何Python如何计算“钱”呢?本文介绍两种方式。

使用定点数

其中一个方式是采用定点数。字面意思看,小数点位置是固定的,即约定机器中所有数据的小数点位置是不变的。这能够确保精度。

import decimal >>> a = 66.9 >>> print(int(decimal.Decimal(str(a)) * decimal.Decimal(100))) 6690

使用库money

首先给出文档地址:

https://pypi.org/project/money/

money库,顾名思义,就是解决“钱”相关的问题。安装很简单,

pip install money

使用起来也比较简单,Money对象入参有两个,一个是数量,一个是货币单位。创建完Money对象,就可以做各种操作了:

from money import Money m = Money(amount='66.9', currency='RMB') print(m * 100)

遗憾的是,没提供内置函数来实现元和角、分的转换。

那Money对象的实现原理是什么呢?可以看一下Money对象的定义:

class Money(object): """Money class with a decimal amount and a currency""" def init(self, amount="0", currency=None): try: self._amount = decimal.Decimal(amount) except decimal.InvalidOperation: raise ValueError("amount value could not be converted to " "Decimal(): '{}'".format(amount)) from None if currency in [None, False, '']: raise ValueError("invalid currency value: '{}'".format(currency)) if not REGEX_CURRENCY_CODE.match(currency): raise ValueError("currency not in ISO 4217 format: " "'{}'".format(currency)) self._currency = currency @property def amount(self): return self._amount @property def currency(self): return self._currency

从代码中可以看出,Money对象有2个属性,一个是_amount,一个是_currency。其中,_amount在初始化时,也是将传入的值转化为定点数,其本质也是依赖于定点数。

所以,后续使用Python计算“钱”时,一定要用定点数,浮点数是算不明白“钱”的哦。

不要太过急功近利, 慢慢玩, 精进.

如果你觉着编程有趣, 成长也就不再痛苦, 祝学习愉快.

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558342791352" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

相关文章

网友评论

    本文标题:万能的Python竟然算不明白“钱”?损失惨重!

    本文链接:https://www.haomeiwen.com/subject/mgtlzqtx.html