[issue24827] round(1.65, 1) return 1.6 with decimal

2017-07-30 Thread Huan Wang

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



[issue24827] round(1.65, 1) return 1.6 with decimal

2017-07-30 Thread Huan Wang

Huan Wang added the comment:

Hi Mark,

Thank you for your reply.

I went over again the answer from Zachary Ware published on 2015-08-08 09:36. I 
got the point that it is better to use string type of number.

>>> from decimal import Decimal, ROUND_HALF_UP
>>> Decimal("1.45")
Decimal('1.45')

>>> Decimal(Decimal("1.45").quantize(Decimal("0.1"), rounding=ROUND_HALF_UP))
Decimal('1.5')

I think it is better to make a tip in the Python tutorial.

--

___
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