简体   繁体   English

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

[英]Python float formatting: fixed total number of digits

I am saving some data with numpy.savetxt(path,array,fmt="%.2f %.2f %.2f %.2f %.2f") and I want the text file to be formated nicely. 我用numpy.savetxt(path,array,fmt="%.2f %.2f %.2f %.2f %.2f")保存了一些数据,我想要很好地格式化文本文件。 I would like to limit the floats to a certain amount of digits, like this: 我想将浮点数限制为一定数量的数字,如下所示:

11.2345 -> 11.2
1.2345 -> 1.23

Currently I get: 目前我得到:

11.2345 -> 11.23
1.2345 -> 1.23

This destroys the layout of the text file. 这会破坏文本文件的布局。

I know that there are a couple of similar questions on SO, however I was not able to apply any of the solutions to numpy.savetxt() . 我知道在SO上有几个类似的问题,但是我无法将任何解决方案应用于numpy.savetxt()

Try using fmt="%9.2f" . 尝试使用fmt="%9.2f" It seems to give nice results for me: 它似乎给我带来了不错的结果:

    25.72    433.54    135.69    898.93
   177.46    120.65    954.13    480.82
   963.45    774.35    289.08     93.64

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

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