在小数分隔符之前格式化python中的float

0 python floating-point formatting tabular

我格式化浮动如下:

logFile.write("Loadcase: %6i %2i %4i %2.2f %2.2f %4f + %4f -> " %(i, freq, amp, cutoff, end_time, penalty_inc, stab_inc))
Run Code Online (Sandbox Code Playgroud)

结果如下:

Loadcase:   5001  1   50 19.00 20.00 0.000000 + 0.000000 -> 0.495386 (ok)
Loadcase:   5002  2   50 9.50 10.00 0.000000 + 0.000000 -> 0.255045 (ok)
Loadcase:   5003  3   50 6.33 6.67 0.000000 + 0.000000 -> 0.151464 (ok)
Loadcase:   5005  5   50 3.80 4.00 0.000000 + 0.000000 -> 0.116979 (ok)
Loadcase:   5010 10   50 1.90 2.00 0.000000 + 0.000000 -> 0.081181 (ok)
Run Code Online (Sandbox Code Playgroud)

我只是希望浮动对齐,并在必要时插入一些空格.我真的基本上希望桌子很好地对齐.我对编写%2.2f的猜测似乎被忽略了.

由于技术原因我不能使用numpy.

Ign*_*ams 7

你误解了什么%2.2f意思.这意味着"给浮动2列总计,并在小数点后显示2个位置".也许你想要的%5.2f.