简体   繁体   English

在bash linux中Ceil仅浮点数

[英]Ceil only floating point numbers in bash linux

In linux bash, I want to get the next integer for a given number only if it is a floating point number. 在linux bash中,仅当它是浮点数时,我想获取给定数字的下一个整数。 I tried with 我尝试过

count=$((${count%.*} + 1));

But with the above code, all the time (even if the number is not floating point), it is giving next integer. 但是使用上面的代码,一直(即使该数字不是浮点数),它始终给出下一个整数。

Expected result : 预期结果 :

345.56 => 346
345.12 => 346
345 => 345

Can anyone help me to find the solution? 谁能帮助我找到解决方案?

Thanks in advance. 提前致谢。

you can use 您可以使用

NUMBER=365
perl -w -e "use POSIX; print ceil($NUMBER/1.0), qq{\n}"

for assigning to a variable 用于分配变量

MYVAR=$(perl -w -e "use POSIX; print ceil($NUMBER/1.0), qq{\n}")

In awk. 在awk。 Some test records: 一些测试记录:

$ cat file    # dont worry about stuff after =>
345.56 => 346
345.12 => 346
345 => 345
345.0
0 
-345.56 => 346
-345.12 => 346
-345 => 345
-345.0

The awk: awk:

$ awk '{print $1 " => " int($1)+($1!=int($1)&&$1>0)}' file
345.56 => 346
345.12 => 346
345 => 345
345.0 => 345
0 => 0
-345.56 => -345
-345.12 => -345
-345 => -345
-345.0 => -345

You'll have to test for the presence of a dot: 您必须测试是否存在点:

case $count in
    *.*) count=$(( ${count%.*} + 1 )) ;;
      *) : nothing to do
         ;;
esac

or 要么

[[ $count == *.* ]] && count=$(( ${count%.*} + 1 ))

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

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