Python浮点格式:固定的总位数

时间:2013-04-14 11:10:15

标签: python numpy floating-point fixed-width

我使用numpy.savetxt(path,array,fmt="%.2f %.2f %.2f %.2f %.2f")保存了一些数据,我想要很好地格式化文本文件。我想将浮点数限制为一定数量的数字,如下所示:

11.2345 -> 11.2
1.2345 -> 1.23

目前我得到:

11.2345 -> 11.23
1.2345 -> 1.23

这会破坏文本文件的布局。

我知道在SO上有几个类似的问题,但我无法将任何解决方案应用于numpy.savetxt()

1 个答案:

答案 0 :(得分:4)

尝试使用fmt="%9.2f"。它似乎给我带来了不错的结果:

    25.72    433.54    135.69    898.93
   177.46    120.65    954.13    480.82
   963.45    774.35    289.08     93.64
相关问题