How to set variable precision for new python format option

后端 未结 2 794
我在风中等你
我在风中等你 2021-01-23 13:38

I am loving the new format option for strings containing variables, but I would like to have a variable that sets the precision through out my script and I am not sure how to do

2条回答
  •  爱一瞬间的悲伤
    2021-01-23 14:26

    Or you can use numbering.

    a = 1.23456789
    some_precision = 5
    out_str = 'a = {0:.{1}f}'.format(a,some_precision)
    print(out_str)
    

    Ouptut:

    a = 1.23457
    

提交回复
热议问题