繁体   English   中英

如何将数字格式化为N个小数,其中N是随机的

[英]How to format a number up to N decimals where N is random

我想加密一个最多N小数的数字。 我使用的过程有时产生的小数小于N所以我需要用左边的零填充其余部分。 我试过这个:

N = 5 # The number of decimals needed for encryption

encrypted = '%0Nd' % (x) # here x is a number used to encrypte the original number 

encrypted N应该是一个修正的数字。

encrypted = '{:0{width}d}'.format(x, width=N)

Python 3.6解决方案与格式字符串:

encrypted = f'{d:0{N}}'

例如,

>>> d = 5
>>> N = 3
>>> f'{d:0{width}}'
005

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM