Recently I came upon this awesome way to prevent python from converting floats to scientific notation:
start = 0.000005
a = f'{start:.10f}'
print(a)
Output:
0.0000050000
I was wondering if there is an automated way to determine how many decimals the number has instead of providing the amount yourself .10f
so I can get this output:
0.000005
You can use the rstrip method to remove the zeros on the right:
start = 0.000005
a = f'{start:.10f}'.rstrip('0')
print(a)
# 0.000005
In case you have no decimal part, it could be better to also remove the '.' when there is no zero left:
start = 5.00000
a = f'{start:.10f}'.rstrip('0').rstrip('.')
print(a)
# 5
just use print(f'{start:f})
without specyfing how many decimal points you want
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.