【问题标题】:universal method for working with currency with no rounding, two decimal places, and thousands separators使用没有四舍五入、两位小数和千位分隔符的货币的通用方法
【发布时间】:2013-03-17 12:55:25
【问题描述】:

我是一个学习 python (2.7.3) 的新手,我得到了一个简单的计算成本的练习,但很快就对真正能够控制或理解正在产生的结果的“精确性”产生了兴趣。如果我在我的计算机上使用计算器,我会得到我认为我想要的结果(即它们看起来非常具体),但如果我使用 python,我似乎必须非常准确地了解如何将格式信息添加到正在使用的值中,并且打印。

我的问题的简短版本是:

是否有一个简单的解决方案可以在我使用返回的货币时使用:

  • 小数点右边两位小数
  • 不进行四舍五入(即基本上只是“截断”小数点后第二位右侧的任何内容)
  • 必要时添加千位分隔符

如果有人想运行它来查看结果和变化,我一直在用解释器(见下面的代码)测试变化。

谢谢。

# basic entry - both are seen as integers
print 'A:  ', 7/3

# test by printing out the types
print 'B:  ', type(7)
print 'C:  ', type(3)

# this is not necessary as they are already integers
print 'D:  ', int(7/3)

# converts the result of 7/3 into a floating number
print 'E:  ', float(7/3)

# defines the float version of 7 by the float version of 3
print 'F:  ', float(7) / float(3)

# coverts the results of the above into a string and assigns to variable
string_var = str(float(7) / float(3))

# print the string
print 'G:  ', string_var

# prints 4 characters of the string, but returns two decimal places
print 'H:  ', string_var[:4]

string_var = str(25.8545678888)

# prints 5 characters of the string, but returns two decimal places
print 'I:  ',string_var[:5]

# import the locale module
import locale
# sets to user's default settings (don't know what empty quotes are)
locale.setlocale( locale.LC_ALL, '' )

#set an arbitrary float number to a variable
float_var = 25.859

# apply locale.currency formatting
print 'J:  ',locale.currency(float_var)

# import the decimal module
from decimal import *

# the decimal version of the variable
print 'K:  ',Decimal(float_var)

# divide the decimal version by 2
print 'L:  ',Decimal(float_var) / 2

# divide the decimal version by the decimal verson of 2
print 'M:  ', Decimal(float_var) / Decimal(2)

# change decimal precision to 6
getcontext().prec = 6

# note there is no change in this value as precision only applied to
# result of calculations, see
#http://stackoverflow.com/questions/6483440/python-decimal-precision
print 'N:  ', Decimal(float_var)

# the following equation returns decimal precision to 6 (6 characters displayed)
print 'O:  ', Decimal(float_var) / 2

# example of 6 characters printed, not decimal places
# to the right of the decimal
print 'P:  ', Decimal(55550) / 130

# if you want to control places to the right of the decimal
# displayed without rounding and show thousand separators if
# necessary ?

编辑:

感谢大家的回复,这是我采用的解决方案,是所有回复的混合:

def make_it_money(number):
    """
    always:
    - shows 2 decimal places
    - shows thousands separator if necessary
    - retains integrity of original var for later re-use
    - allows for concise calling
    """
    import math
    return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

# tests

var1 = 25.867
var2 = 25.864
var3 = 25.865
var4 = 2500.7869

print make_it_money(var1)
print make_it_money(var2)
print make_it_money(var3)
print make_it_money(var4)

【问题讨论】:

  • 你真的需要看看Decimal module
  • 谢谢我已经尝试过了,如上面的代码所示,但我无法理解文档足以实现我在小数点右侧的 2 个小数位之后的结果,没有四舍五入, 如有必要,使用千位分隔符。

标签: python python-2.7 currency-formatting


【解决方案1】:

使用format() function 格式化floatDecimal 类型:

format(value, ',.2f')

导致:

>>> format(12345.678, ',.2f')
'12,345.68'

, 在输出中添加一个逗号作为千位分隔符。

您通常希望在浮点数或Decimal 计算后不加选择地向下舍入。如果您觉得自己必须这样做,请在格式化之前明确地这样做:

>>> import math
>>> format(math.floor(12345.678 * 100) / 100, ',.2f')
'12,345.67'

【讨论】:

  • 谢谢,有没有办法不进行任何舍入?即只是截断小数点后第二位右边的任何内容?例如,print format(25.859, ',.2f') 打印出 25.86。我只想显示 25.85 而不进行任何舍入(只是截断)。我知道我可以将其转换为字符串并执行此操作,但如果可能的话,最好保留数字的完整性。
【解决方案2】:

Python 从 C 中继承了很多内容,因此您可以使用 print %.2f 语法在逗号后打印一个只有 2 个值的浮点数,这将四舍五入该值。

千位分隔符似乎也很简单,请查看以下问题/答案:

https://stackoverflow.com/a/5513747/1093485

您还可以导入现有模块,例如 python-money (https://pypi.python.org/pypi/python-money)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2015-03-15
    • 1970-01-01
    • 2015-07-31
    • 2011-06-21
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多