How to round a floating point number up to certain decimal place in Python?

0 votes

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 by ariaholic
• 7,340 points
46 views

1 answer to this question.

0 votes

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 charlie_brown
• 7,720 points

Related Questions In Python

0 votes
2 answers

How to calculate square root of a number in python?

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

answered Apr 2 in Python by anonymous
246 views
0 votes
1 answer

Convert floating point number to a certain precision

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

answered Oct 18, 2018 in Python by SDeb
• 13,180 points
43 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

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

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 332 views
0 votes
1 answer

How to Print a List in Python

ou are using Python 2.x syntax with ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
93 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
136 views