Answers:
您可以尝试以下方法:
$ awk 'BEGIN{printf "%3.0f\n", 3.6}'
4
我们的格式选项包括两个部分:
3
:表示输出将填充为3个字符。.0f
:表示输出将没有精度,表示四舍五入。从中man awk
,您可以看到更多详细信息:
width The field should be padded to this width. The field is normally padded
with spaces. If the 0 flag has been used, it is padded with zeroes.
.prec A number that specifies the precision to use when printing. For the %e,
%E, %f and %F, formats, this specifies the number of digits you want
printed to the right of the decimal point. For the %g, and %G formats,
it specifies the maximum number of significant digits. For the %d, %o,
%i, %u, %x, and %X formats, it specifies the minimum number of digits to
print. For %s, it specifies the maximum number of characters from the
string that should be printed.
使用%f
格式说明符,您的(浮点数)数字将根据您的指定自动舍入。例如,要将值四舍五入为整数,请使用
$ awk 'BEGIN { printf("%.0f\n", 1.49); }'
1
$ awk 'BEGIN { printf("%.0f\n", 1.5); }'
2
如果您想要更多的尾随数字,只需更改精度。
BEGIN
块中,则不是。我首先在正常人体中测试了表达,因此进行了测试。谢谢,@ Gnouc。
/dev/null
必要吗?