Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have a floating-point number, say 135.12345678910. I want to concatenate that value to a string but only want 135.123456789. With print, I can easily do this by doing something like:

print "%.9f" % numvar

with numvar being my original number. Is there an easy way to do this?

1 Answer

0 votes
by (106k points)

By using round method, you can convert a floating-point number to a certain precision, and then copy to string see the code below:-

>>> numvar = 135.12345678910 

>>> str(round(numvar, 9)) 

'135.123456789'

To know more about this you can have a look at the following video tutorial:-

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...