rounding - How to round Python Decimal instance -


how round python decimal instance specific number of digits while rounding nearest decimal?

i've tried using .quantize(decimal('.01')) method outlined in docs, , suggested in previous answers, doesn't seem round correctly despite trying different round_ options. i've tried setting getcontext().prec, seems control total number of digits in entire number, not decimals.

e.g. i'm trying like:

assert decimal('3.605').round(2) == decimal('3.61') assert decimal('29342398479823.605').round(2) == decimal('29342398479823.61') assert decimal('3.604').round(2) == decimal('3.60') assert decimal('3.606').round(2) == decimal('3.61') 

i think need use decimal.round_half_up option quantize want.

>>> x in ('3.605', '29342398479823.605', '3.604', '3.606'):     print x, repr(decimal(x).quantize(decimal('.01'), decimal.round_half_up))  3.605 decimal('3.61') 29342398479823.605 decimal('29342398479823.61') 3.604 decimal('3.60') 3.606 decimal('3.61') 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -