In linux bash, I want to get the next integer for a given number only if it is a floating point number. 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 perl
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. 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 '{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 ))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.