[英]How to format a number up to N decimals where N is random
I want to encrypt a number up to N
decimals. 我想加密一个最多
N
小数的数字。 The process I use sometimes generates less decimals than N
so I need to pad the rest with zeroes to the left. 我使用的过程有时产生的小数小于
N
所以我需要用左边的零填充其余部分。 I tried this: 我试过这个:
N = 5 # The number of decimals needed for encryption
encrypted = '%0Nd' % (x) # here x is a number used to encrypte the original number
but N
in encrypted
should be a number priory determined. 但
encrypted
N
应该是一个修正的数字。
encrypted = '{:0{width}d}'.format(x, width=N)
Python 3.6 solution with format strings: Python 3.6解决方案与格式字符串:
encrypted = f'{d:0{N}}'
For example, 例如,
>>> d = 5
>>> N = 3
>>> f'{d:0{width}}'
005
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.