Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 08-16-2004, 12:39 PM   #1
Registered: Oct 2003
Distribution: Redhat 9
Posts: 138

Rep: Reputation: 15
how to compare decimals

I am writing a script where I need to check if a decimal number is higher than another given decimal number using an if statement.

I have the if setup so far as:

if [ $<dec. num> -lt 0.02 ]; then

since the "-lt" operator only does integer I get an error that looks like this:

[: .000066041946411: integer expression expected

my second idea is that I should multiply the first number by a thousand and then round it off to create an integer. I googled "round decimal" and I get nothing productive.

Can someone help me either compare the decimal number or tell me how I round a decimal? I am guessing that rounding the decimal has something to do with the "bc" command.
Old 08-16-2004, 01:09 PM   #2
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
You could use the arbitrary precision calculator bc if it is installed on the system. It accepts equations like 1.2 < 1.3 and outputs 1 for true or 0 for false. e.g:
bc -q
bc -q starts bc without a welcome message.

You can echo expressions to bc from a script and receive the output:
echo "1.2 > 1.3" | bc
gives the result 0. Thus you can include piped input (as above) as part of a script.
(in Perl):
if(`echo \"1.2 > 1.1\" | bc` == 0){
 print "Something here...";
Sorry, I don't do much bash scripting, but the principle is the same.
Old 08-16-2004, 02:43 PM   #3
Registered: Oct 2003
Distribution: Redhat 9
Posts: 138

Original Poster
Rep: Reputation: 15
I tried what you put and it worked. Thank you so much!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
beyond compare 4 linux novazem Linux - Software 14 12-06-2008 10:22 PM
Can I use a lot of decimals in my C program? Golradir Programming 14 07-04-2004 12:40 PM
compare string in C++ danxl Programming 3 11-02-2003 02:14 PM
directory compare tmoorman Linux - Newbie 2 09-26-2003 01:45 PM WeNdeL Linux - Newbie 0 01-17-2003 12:00 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:13 AM.

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