+2 votes
2 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 (46.1k 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 (107k 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
+2 votes
2 answers
+3 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...