Huan Wang added the comment:
Hello,
I was confused by the decimal module. The problem is that I want to
from decimal import Decimal, ROUND_HALF_UP
def rounded(number, n):
''' Round the digits after the n_th decimal point by using
decimal module in python.
For example:
2.453 is rounded by the function of deal_round(2.453, 1),
it will return 2.5.
2.453 is rounded by the function of deal_round(2.453, 2),
it will return 2.45.
'''
val = Decimal(number)
acc = str(n) # n = 0.1 or 0.01 or 0.001
return Decimal(val.quantize(Decimal(acc), rounding=ROUND_HALF_UP))
for x in np.arange(1.0, 4.01, 0.01):
rounded_val = rounded(x, 0.1)
print("{:}\t{:}".format(x, rounded_val))
The results obtained from the numpy array looks fine, but if I directly used
rounded(1.45, 0.1), it yielded Decimal('1.4'), rather than Decimal('1.5').
I think it would be a bug.
--
nosy: +Huan
versions: +Python 3.5 -Python 3.3
___
Python tracker
<http://bugs.python.org/issue24827>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com