to convert string to integer value

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 1
Reputation: justsomerat is an unknown quantity at this point 
Solved Threads: 0
justsomerat justsomerat is offline Offline
Newbie Poster

to convert string to integer value

 
0
  #1
Jun 15th, 2005
How to compare a number stored as string to an integer value in shell scripting? Here is what I have,

if [$temp -ge 2.5] then;

when I run the above code, it gives the error as "Integer Expression expected".
Here the temp variable contains the number "3.0" as a string, I want to compare the value of temp is greater or equal to the number 2.5.
How can I do it.

Please reply soon.

Thanks in advance.

Shiva
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: to convert string to integer value

 
0
  #2
Jun 16th, 2005
an interger is a whole number, with no decimal point. That is why it is telling you an integer is expected.

either the number 2.5 or 3.0 will cause it to fail.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 2
Reputation: Cosla is an unknown quantity at this point 
Solved Threads: 0
Cosla Cosla is offline Offline
Newbie Poster

Re: to convert string to integer value

 
0
  #3
Mar 17th, 2006
Does any one have the pseudocode for this problem?? If you do plaz contact me

Thanx
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 29
Reputation: vartotojas is an unknown quantity at this point 
Solved Threads: 0
vartotojas vartotojas is offline Offline
Light Poster

Re: to convert string to integer value

 
0
  #4
Jun 9th, 2006
Here's some code for C# that works:

You can use doubles to hold numbers with decimal places.

if [$temp.convert.todouble() -ge 2.5] then;

that should solve your problem.
Last edited by vartotojas; Jun 9th, 2006 at 11:56 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: to convert string to integer value

 
0
  #5
May 30th, 2007
Try putting quotes around 2.5 and $temp like so:
if ["$temp" -ge "2.5"] then;
otherwise set a variable equal to 2.5 and test that:
SOME_VALUE=2.5
if [$temp -ge $SOME_VAULE] then;
This may require quotes too so...

These suggestions are based on BASH shell scripting which is what this kind of looks like, so your mileage may vary...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC