user3611091
user3611091

Reputation: 47

Change number of numbers after dot

I wrote a simple python script that counts the time it takes to send a HTTP request using urllib2.

After i count the the time, it prints the number of time. It looks like this: 2.582848693

I dont really need all the decimal numbers, I only need 1 decimal number. I need it would be : 2.5

How can it be done using Python? Thanks!

Upvotes: 0

Views: 1378

Answers (2)

Try with:

x = 2.582848693
y = int(x*10)/10.0

y contains a copy of x value with just one decimal digit and you can print it as well as use it.

You could also just print int(x*10)/10.0.

Upvotes: 0

AlbertFerras
AlbertFerras

Reputation: 726

Use string formatting as follows:

print "%.1f" % total_time

.1f means that the parameter should be printed as a float with 1 decimal

Or you can write this to print it with 2 decimals and some extra text:

print "The total amount of time is %.2f" % total_time

Upvotes: 2

Related Questions