This can be accomplished by:
print("{:.6f}".format(your value here));
Answer from user3206656 on Stack OverflowForce python to print a certain number of decimal places - Stack Overflow
python - How to print float to n decimal places including trailing 0s? - Stack Overflow
printing - Python print a float with a given number of digits - Stack Overflow
Specifying number of digits after decimal point?
You can use the f-string f'{value:.6f}'.
Example:
value = 0.234
print(f'{value:.6f}')
value = 1
print(f'{value:.6f}')
value = 0.95269175
print(f'{value:.6f}')
Output:
0.234000
1.000000
0.952692
Also, in the answer linked in a comment, there was reference to :g. That can work, but probably not in this situation, because g may print scientific notation where appropriate, and discards insignificant zeroes. Consider a slightly modified example using g:
value = 0.234
print(f'{value:.6g}')
value = 1
print(f'{value:.6g}')
value = 0.000000000095269175
print(f'{value:.6g}')
Output:
0.234
1
9.52692e-11
You can also use basic string formatting:
a = 3e-06
# Outputs 0.000003
print('%.6f' % a)
# Outputs 0.000003000000
print('%.12f' % a)
For Python versions in 2.6+ and 3.x
You can use the str.format method. Examples:
>>> print('{0:.16f}'.format(1.6))
1.6000000000000001
>>> print('{0:.15f}'.format(1.6))
1.600000000000000
Note the 1 at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.
For Python versions prior to 2.6 (at least back to 2.0)
You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):
>>> print '%.16f' % 1.6
1.6000000000000001
>>> print '%.15f' % 1.6
1.600000000000000
The cleanest way in modern Python >=3.6, is to use an f-string with string formatting:
>>> var = 1.6
>>> f"{var:.16f}"
'1.6000000000000001'
If you want to "avoid" the last 1, which occurs at the 15th decimal place because of how floating point numbers work, you can convert the float first into a string representation and then into a Decimal:
>>> from decimal import Decimal
>>> f"{Decimal(repr(var)):.16f}"
'1.6000000000000000'
Note that if you are working with numbers that need 15 decimal places of precision, you should not be using floats in the first place but should build your solution around Decimals from the get-go.
The formating method for both integer and decimal is
>>> '{:06.2f}'.format(3.141592653589793)
'003.14'
the part before the . (6 here) denotes the total length of padding (including the .), and after the . (2fhere) denotes digits after decimal point.
Hope it helps. checkout the link.
You can try to do something like this
print('{:.6}'.format(val))
If I do num1 = 2.50 and then look at the value of num1, I get 2.5 instead of 2.50.
Is there a way to force 2.50 instead of 2.5 without converting to a string and adding the appropriate amount of trailing zeroes?
Okay, two parts:
first, to format a number:
"{number:06}".format(number=100)
will give you '000100'. But before that, we have to round.
EDIT: This solution is much more elegant:
import math
def rep(number):
rounded = 10**(math.floor(math.log(number,10)-math.log(0.5,10)))
return "{number:06}".format(number=int(rounded))
Let's see:
>>> print rep(100)
000100
>>> print rep(1000)
001000
>>> print rep(501)
001000
>>> print rep(499)
000100
>>> print rep(500)
000100
OLD Version for future reference end educational delight:
(It's still faster as it doesn't involve any log operations)
Here's a neat little trick: round(501) will round to the first decimal digit, but round(501, -1) will round to the 10^1 digit (so the result is 500.0), round(501, -2) to the 10^2 (the result still being 500.0), and round(501, -3) will round up to 1000.
So we want 500 to be rounded up to 1000, but 53 to be rounded up to 100. Well, here's how we do it:
number = 521
rounded = round(number, -len(str(number)))
So, since the string describing number is three characters long, we round to -3.
However, this rounds up perfectly, but if we're rounding down, it always rounds down to 0. Let's just catch this case:
def rep(number):
rounded = round(number, -len(str(number)))
if not rounded: # 0 evaluates to False
rounded = 10**(len(str(number))-1)
return "{number:06}".format(number=int(rounded))
zfill also can help you
>>> str(10).zfill(6)
'000010'