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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can use the
format
method on strings to specify how many decimal places you want to represent:But even better would be to use the
decimal module
for representing money, since representation issues with binary floats can give you slightly off results if you're doing arithmetic. The documentation for that module mentions some of those issues specifically - one of the most interesting ones for money applications is:When working with money, always represent money using the Decimal class.
http://docs.python.org/2/library/decimal.html
result
The proposed solutions do not work when the magnitude of the number is not known in advance, which is common in scientific applications rather than in money-related ones. I give an alternative solution for those, like me, coming to this question looking for the scientific case.
For example, if I want to print
x = 1.500e-4
to three significant digits (common situation when dealing with measurements with a given uncertainty), the following command obviously does not give the correct result:Here I used the modern Python 3.6+ f-strings for the formatting.
One may think of using the
g
format specifier, but this also does not give the desired result to three significant digits as the trailing zero is omitted:The correct answer can be obtained using the
g
format specifier together with a rather obscure option, the hash character#
, of the format-specification mini language, in the following way:This formatting also works unchanged in the simpler case of the original question: