在我的 ksh 脚本中我只需要计算整数
有时我会得到 float ,例如 3.49 或 4.8...等
所以我需要根据以下规则(示例)将 float 转换为整数
3.49 will be 3
2.9 will be 3
4.1 will be 4
23.51 will be 24
982.4999 will be 982
10.5 will be 11 ( this example if float is .5 then it will roundup )
请建议如何在 ksh 或 awk 或 perl 中执行此操作
或者
可以在我的 ksh 脚本中运行的任何其他语言
最佳答案
经过简短的 google session 后,我发现 printf
似乎能够完成这项工作,至少在 bash 中是这样(找不到执行 ksh 的在线解释器)。
printf "%0.f\n" 4.51
5
printf "%0.f\n" 4.49
4
注意:perl 可能有点矫枉过正,如 Marius 所说,但这里有一个 perl 方法:
perl 模块 Math::Round似乎可以胜任这项工作。
一行:
perl -MMath::Round -we 'print round $ARGV[0]' 12.49
脚本:
use v5.10;
use Math::Round;
my @list = (3.49, 2.9, 4.1, 23.51, 982.4999);
say round $_ for @list;
脚本输出:
3
3
4
24
982
关于linux + ksh + 向下舍入或向上舍入 - float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8777070/