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.
Oct 8, 2018 in Python by aryya
• 7,460 points
2,361 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
1 answer

How to check if a float value(decimal number) is between range(0 to 10) in python

Hello,  A function that returns True for an integer number (int or ...READ MORE

answered Sep 14, 2020 in Python by Niroj
• 82,840 points
7,106 views
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, 2019 in Python by anonymous
5,858 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,300 points
666 views
–1 vote
2 answers
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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
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
1,067 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
1,648 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP