Message299493
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. |
|
Date |
User |
Action |
Args |
2017-07-30 09:00:01 | Huan | set | recipients:
+ Huan, valhallasw, umedoblock, zach.ware |
2017-07-30 09:00:00 | Huan | set | messageid: <1501405200.93.0.756387412251.issue24827@psf.upfronthosting.co.za> |
2017-07-30 09:00:00 | Huan | link | issue24827 messages |
2017-07-30 09:00:00 | Huan | create | |
|