0

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!

user3611091
  • 47
  • 1
  • 8

2 Answers2

2

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
AlbertFerras
  • 726
  • 5
  • 14
0

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.