ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The value in VAL_5Y is 516.23125 which iam getting after grep from a file so its stored as a String so my task is to convert the VAL_5Y to integer so that i can use in if loop . How to do that . To convert to int i tried multiplying by 1 which is displayed in error .
Pretty sure that's a floating-point number, not an integer. Difference is the decimal point (.) ... You may also need to convert it from a string but maybe not.
Matter of fact, I don't even know if you can use floats in bash scripts... You are using a bash script correct?
Well as this number is not an integer you would have to either truncate or round as bash doesn't do floating point easily.
My solution was to truncate like so:
Code:
VAL_5Y=516.23125
VAL_TEST=$(echo $VAL_5Y | sed 's/\..*//')
if [[ $VAL_TEST -eq 0 ]]
then
<do blah>
else
<do blah2>
fi
Obviously for rounding you would need to construct your test. You also have the choice of using bc but I am not suitably skilled to explain how.
its a bash script and the number can be floating point as in this case.
my main concern is to check if number is greater than or equal to zero. surely i can try ryan and grail approach.Very high probability that the number will have left part of decimal point so surely can try out truncating post decimal and check for zero condition.
VAL_5Y="516.23125"
IFS='.' read int dec <<< "$VAL_5Y"
if (( dec ))
then echo "VAL_5Y is greater than 0"
elif (( int >= 0 ))
then echo "VAL_5Y is greater than or equal to 0"
else echo "VAL_5Y is NOT greater than o equal to 0"
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.