I'm trying to display the output of my addition to more than 2 decimal places.
import time
import random
max_number = 1000000.0
random_time = random.randrange(1, max_number-1) / max_number
range_key = int(time.time()) + random_time
range_key
>>> 1347053222.790799
print range_key
>>> 1347053222.79
How can I print the full number? If this were a function, how could I return the full number?
You already have the full number. Use a custom format specifier on output.
When turning a float into a string (printing does this automatically), python limits it to only the 12 most significant digits, plus the decimal point.
When returning the number from a function, you always get the full precision.
To print more digits (if available), use string formatting:
That defaults to 6 digits, but you can specify more precision:
Alternatively, python offers a newer string formatting method too:
You'll find that if you hard-code the number of digits to print, you'll sometimes get too few or worse yet go past the floating-point precision and get meaningless digits at the end. The standard floating point number only has precision to about 15 digits. You can find out how many of those digits are to the right of the decimal point with this simple formula:
Using that you can create an appropriate
%f
format: