LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2014, 05:00 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
How to compare a decimal variable with a number?


tempr in my code is a decimal number like 23.8!
I want to check if it is less than -20 or great than 50 or not! my
Code:
if [ "$hour" != "$ch" ] || [ "$min" -lt $((cm-10)) ] || [ "$min" -gt $(($cm+10)) ] || [ $(echo "$tempr < -20" | bc -l ) ] || [ $(echo "$tmepr > 50" | bc -l ) ]
        then
 
Old 03-03-2014, 05:18 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Try it in little steps:

step #1:
Code:
NUM=3.212; INT=${NUM%.*}; echo $INT
if [ $INT -lt 4  -a $INT -ge 2 ]; then
    echo "so far so good"
fi;
 
1 members found this post helpful.
Old 03-03-2014, 05:43 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
The problem is the if statement with a lot of reverse logic in it; if one of the statements evaluates to 'true', it will stop evaluating the rest of the statements; Question could also be: which statement to evaluate.

Code:
if [ "$hour" != "$ch" ] 
then
...
fi
So, if $hour equals $ch, the next test has to be evaluated, otherwise it is already true and doesn't become more or less true; So be careful linking all those tests together.

Then again, for the original question posted, bc has a good thing for you:

Code:
cur_temp="20.5"
tst_temp="20"
if [ $(echo "$cur_temp > $tst_temp" | bc -l) ]
then
     echo "It's more than 20."
else
     echo "It's less than 20."
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find the first decimal number in a string ? 5883 Programming 3 08-08-2013 06:48 PM
convert number (not hex) into Decimal number drManhattan Programming 10 10-15-2011 08:53 PM
[SOLVED] How to compare negative and decimal numbers satishchahar Programming 16 09-21-2011 10:17 AM
how to compare a variable with a number? pdklinux79 Linux - Newbie 2 06-10-2008 07:02 PM
Compare Decimal Values rbautch Linux - General 10 04-22-2008 04:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration