Suppose I am having 8.8333333333333339 and I want to convert it to 8.84, how can I accomplish this in python? round(8.8333333333333339, 2) gives 8.83 and not 8.84. I am new to python or programming in general.

I don't want to print it as a string, the result will be further used. For more information on the problem please check Tim Wilson's Python Programming Tips: Loan and payment calculator. Oct 8, 2018 in Python 44 views

1 answer to this question.

This is normal (and has nothing to do with Python) because 8.83 cannot be represented exactly as a binary float, just as 1/3 cannot be represented exactly in decimal (0.333333... ad infinitum).

If you want to ensure absolute precision, you need the decimal module:

>>> import decimal
>>> a = decimal.Decimal("8.833333333339")
>>> print(round(a,2))
8.83 answered Oct 8, 2018 by
• 7,720 points

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

Convert floating point number to a certain precision

With Python < 3 (e.g. 2.6 [see ...READ MORE

How to filter out any digit that contains odd number in a range of number in Python?

n = list(range(10)) b = list(filter(lambda i:i%2!=0,n)) print(b) READ MORE

How can i perform rounding off a decimal number in python?

Can you provide the code for this? READ MORE

how do i change string to a list?

suppose you have a string with a ...READ MORE

how can i randomly select items from a list?

if you google it you can find. ...READ MORE

+1 vote

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE