Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
%5.3fの範囲を満たすために、浮動小数点数をbashで印刷し、ゼロを埋め込んでもらいたいと思います。printf関数を知っています。
私の問題は次のとおりです。
printf "0%5.3f\n" 3.00
予想通り、
03.000
しかし、ライン
printf "0%5.3f\n" 23.00
代わりに与える
023.000
もちろん、それは私が望んでいることではありません。
なにか提案を?
あなたは0 後に置く必要があります%:
0
%
printf "%06.3f\n" 23.00
また、最小フィールド幅を6に増やしたことに注意してください。そうしないと、パディングは発生しません(小数点以下3桁、1ドット、小数点の前に1桁だけが残ります)。
どちらかといえば、0はパーセント記号の右側にある必要があります。Linuxシステムを使用していませんが、試してみてくださいprintf "%05.3f\n" 23.00。
printf "%05.3f\n" 23.00