Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
3 views
in Python by (2.6k points)
edited by

How can I round off b to 16.75

>>> b
16.749999999999999
>>> round(, 2)
16.749999999999999

I tried round function but it doesn't worked.

2 Answers

0 votes
by (46k points)
edited by

You can use round() :

>>> round(16.749999999999999, 2)
16.75

This works well for all Python versions 2.7 or above. Cheers. 

0 votes
by (106k points)
edited by

For limiting floats to two decimal points you can use the simplest approach which is to use the format() function.

An example is as follows:

a = 13.949999999999999 

format(a, '.2f') 

13.95

The above code will produce a float number as a string rounded to two decimal points.

You can use the following video tutorials to clear all your doubts:-

Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...