DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Legacy and Other Languages (http://www.daniweb.com/forums/forum42.html)
-   -   to convert string to integer value (http://www.daniweb.com/forums/thread25855.html)

justsomerat Jun 15th, 2005 1:41 pm
to convert string to integer value
 
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

shanenin Jun 16th, 2005 2:42 pm
Re: to convert string to integer value
 
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.

Cosla Mar 17th, 2006 9:02 pm
Re: to convert string to integer value
 
Does any one have the pseudocode for this problem?? If you do plaz contact me

Thanx

vartotojas Jun 9th, 2006 11:50 am
Re: to convert string to integer value
 
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.

mittelgeek May 30th, 2007 11:40 pm
Re: to convert string to integer value
 
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...


All times are GMT -4. The time now is 1:08 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC