how to compare string integer with an integer?

hi,

how to I do this?

i="4.000"
if [ $i -lt 0 ] ; then
   echo "smaller"
fi

how do I convert the "4.000" to 4?

Thanks!

If you are bot bothered about the decimal values, you can just strip it off using...

val=${i%%.*}

Or bc can be handy...

i=4.000
ret=$( echo "$i < 0" | bc )
echo $ret
0

i=-1.000
ret=$( echo "$i < 0" | bc )
echo $ret
1

man bc

--ahamed

1 Like

this may help you.

1 Like

Why use awk?

j=$(printf "%.0f" "$i")

ksh lets you just typeset it to an integer if you don't care about what is to the right of the decimal place.

#!/bin/ksh

integer i="4.000"

if [ $i -lt 0 ] ; then
   echo "smaller"
fi

exit 0