在python中保持尾随零 [英] Keep trailing zeroes in python

查看:78
本文介绍了在python中保持尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个代表金钱的课程,而我遇到的一个问题是"1.50" != str(1.50). str(1.50)等于1.5,突然之间等于POOF. 45美分消失了,现在的金额是1美元和5美分.不是一美元和五十美分.我有什么办法可以防止str这样做,还是我做错了什么?这是Python 2 BTW.

I am writing a class to represent money, and one issue I've been running into is that "1.50" != str(1.50). str(1.50) equals 1.5, and alll of a sudden, POOF. 45 cents have vanished and the amount is now 1 dollar and 5 cents. not one dollar and 50 cents. Any way I could prevent str from doing this, or am I doing something wrong? This is Python 2 BTW.

推荐答案

使用货币时,请始终使用Decimal类表示货币.

When working with money, always represent money using the Decimal class.

http://docs.python.org/2/library/decimal.html

这篇关于在python中保持尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆