Unexpected floating-point representations in Python

前端 未结 3 1795
后悔当初
后悔当初 2020-12-21 03:44

Hello I am using a dictionary in Python storing some cities and their population like that:

population = { \'Shanghai\' : 17.8, \'Istanbul\' : 13.3, \'Karach         


        
3条回答
  •  悲&欢浪女
    2020-12-21 04:24

    You have to use decimal.Decimal if you want to have the decimal represented exactly as you specified it on any machine in the world.

    See the Python manual for information: http://docs.python.org/library/decimal.html

    >>> from decimal import Decimal
    >>> print Decimal('3.14')
    3.14
    

提交回复
热议问题