mar*_*arc 2 python bash perl rounding
我试图使用awk在bash中舍入几个小数值.例如:如果值为6.79
awk 'BEGIN {rounded = sprintf("%.0f", 6.79); print rounded }'
Run Code Online (Sandbox Code Playgroud)
这让我回报7.
有没有办法我可以舍入到最接近的整数(1,2,3,..),但步骤为0.5(0,0.5,1,1.5,2,2.5 ......)
任何在python或perl中工作的替代方法也都可以.python中的当前方式
python -c "from math import ceil; print round(6.79)"
Run Code Online (Sandbox Code Playgroud)
也返回7.0
Perl解决方案:
perl -e 'print sprintf("%1.0f",2 * shift) / 2' -- 6.79
7
Run Code Online (Sandbox Code Playgroud)
诀窍很简单:将数字乘以2,绕过它,然后再划分.