Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] How to compare a decimal variable with a number?
 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

 03-03-2014, 05:00 AM #1 massy Member   Registered: Nov 2013 Distribution: CentOS 6.4 Posts: 197 Blog Entries: 1 Rep: 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```
 03-03-2014, 05:18 AM #2 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 3,436 Rep: 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.
 03-03-2014, 05:43 AM #3 Ramurd Member   Registered: Mar 2009 Location: Rotterdam, the Netherlands Distribution: Slackwarelinux Posts: 682 Rep: 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```

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post 5883 Programming 3 08-08-2013 06:48 PM drManhattan Programming 10 10-15-2011 08:53 PM satishchahar Programming 16 09-21-2011 10:17 AM pdklinux79 Linux - Newbie 2 06-10-2008 07:02 PM rbautch Linux - General 10 04-22-2008 04:11 AM

LinuxQuestions.org

All times are GMT -5. The time now is 12:48 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -