Hi,
I would like test if a number is a decimal number or not
Hi,
I would like test if a number is a decimal number or not
one way
isdec()
{
echo "$1" | tr -dc '[:digit:]' | read value
if [[ $(( ${#1} - ${value} )) -eq 1 ]] ; then # all numbers except one character
[[ echo "$1" | grep -q '\.' ]] && echo 'ok' || echo 'not ok' # one char is .
fi
}
var=zz3
isdec $var
var=1.234
isdec $var
Try...
echo $number | awk '$0==$0+0 && match($0,0)!=1 {print "Decimal number"}'
$0==$0+0 checks that its a number(but not Hexadecimal) and match($0,0)!=1 checks its not octal.
Hi, i try something like this :
nombre=13.5
if (test $nombre = {*.*}); then
echo "ici"
else
echo "la bas"
fi
exit 0;
But it doesn't works..... Whatever value I put in nombre, it returns "la bas". I think I don't have the correct syntaxe to say " a string composed of blablabla.blablabla" (where blabla are digits)
I tried
,
and many other things, but it still don't works.
so is it possible to do what I want to do? If yes, could someone tell me the right way to write it?
regards
Sorry, i would like know , after a division , if result is a number integer or not,
( 300 and 301 are value)
echo 300 / 2 | bc -l
150.00000000000000000000
--> it 's ok
echo 301 / 2 |bc -l
150.50000000000000000000
--> it's ko
if [ `echo $num / 2 | bc -l | grep -c "[0-9]*\.00000000000000000000" ` = 1 ]
then
echo "OK"
else
echo "KO"
fi
Something like this?
echo <number> | awk '$0-int($0){print $0 " = KO";next}{print $0 " = OK"}'
ksh93 supports the libm int() function i.e.
#!/bin/ksh93
d=150.50
if (( int(d) == d ))
then
print "Integer"
else
print "Not integer"
fi
Also case is nice method to test this kind of needs. Here is some test and output
#!/bin/ksh
for n in 100 +100 -100 100.00 -100.00 +100.00 100.00.00 abc 10-23.45
do
num=0
echo -n "$n:"
case "$n" in
+([0-9])) echo "int >= 0" ; num=1 ;;
++([0-9])) echo "int >= 0" ; num=1 ;;
-+([0-9])) echo "int < 0" ; num=1 ;;
+([0-9]).+([0-9])) echo "float >= 0" ; num=1 ;;
++([0-9]).+([0-9])) echo "float >= 0" ; num=1 ;;
-+([0-9]).+([0-9])) echo "float <= 0" ; num=1 ;;
*) echo "not num" ;;
esac
done
num1=300
num2=2
if [ $(( $num1 % $num2 )) -eq 0 ]
then
echo "OK"
else
echo "KO"
fi