Can expr deal with decimals?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can expr deal with decimals?
# 1  
Old 12-14-2012
Can expr deal with decimals?

Hello,
Im trying to work on a small script for a formula converting temperatures, Celsius to fahrenheit.

I have a formula, and it works.... it works every way I try it. But it keeps giving me the wrong results every time with expr.

The formula for C to F would be:
divide by 5, multiply by 9, add 32.

So..... simple code here
Code:
read c
f= `expr $c / 5 \* 9 + 32`
echo $f

The problem is, its never correct because expr can't seem to add the decimals right.

For instance 32 degrees celcius.
f=`expr 32 / 5 \* 9 + 32` gives me 86 for some reason. It should give 89 (or 89.6)

I have also tried it another way, which seems odd, but still should give correct results.
Code:
f=`expr 9 / 5 \* $c + 32`

Again, I have tested, it should be 89, but it ends up giving me 64.


Because expr 9 / 5 is giving me 1, instead of 1.8
So 1 * 32 = 32 + 32 = 64.
It should be 1.8 * 32 = 57.6 + 32 = 89.6

It seems like my problem is the fact that expr won't account for decimals.
Is there a way to get it to acknowledge the decimals?
Or is this not possible with the expr command?

Thanks a lot

Last edited by Scrutinizer; 12-14-2012 at 01:20 AM.. Reason: code tags
# 2  
Old 12-14-2012
I think expr does not work with floating point number. Try awk instead:-
Code:
echo | awk ' { printf "%0.2f\n", (57.6 + 32); } '

Or use bc
Code:
echo "scale=2; 57.6 + 32" | bc

# 3  
Old 12-14-2012
expr does not "do" floating point numbers (decimal numbers).

Code:
bc -l

does.

Code:
 echo "1.356 * 1.2" | bc -l


The awk command does double precision (15 digits of floating point) as well.

Code:
echo "13 14.112" | awk '{print $1 * $2}'

# 4  
Old 12-14-2012
Thanks a lot,

Im having some trouble with bc too.


Code:
echo $c / 5 \* 9 + 32 | bc
echo 32 / 5 \* 9 + 32 | bc

gives me 86 (instead of 89)

Im not sure how it is rounding 89.6 to 86..... its making no sense to me Smilie

adding the -l
Code:
echo 32 / 5 \* 9 + 32 | bc -l

gives me 89.600000000000000000
Which is correct. But for the output file, I just need the whole number. (89)

Last edited by Scrutinizer; 12-14-2012 at 01:18 AM.. Reason: code tags
# 5  
Old 12-14-2012
You have to use scale to get accurate results. Here is what is happening:-

See the difference in results with scale and without scale:-
Code:
echo '( 32 / 5 )' | bc
6
echo '( 32 / 5 ) * 9 ' | bc
54

Code:
echo 'scale=2; ( 32 / 5 )' | bc
6.40
echo 'scale=2; ( 32 / 5 ) * 9' | bc
57.60

I hope you understood how 89.6 got rounded to 86 Smilie
This User Gave Thanks to Yoda For This Post:
# 6  
Old 12-14-2012
That explains it......
Now I see why it rounded it like that......

but even if I do a scale=1, I get 89.6
scale=0 obviously gives me the rounded "86"

So is there not a way to just get 89?
# 7  
Old 12-14-2012
Code:
 
$ res=$(echo 32 / 5 \* 9 + 32 | bc -l); 
$ echo ${res%%.*}
89

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to find the field has more than 2 decimals

Hi Gurus, I have below sample file, I need find the line which 2rd field has more than 2 decimals. in sample file, I need to find xyz, 123456.789 abc, 1234.45, def xyz, 123456.789, xxx bce, 1234.34, xxx thanks in advance (13 Replies)
Discussion started by: ken6503
13 Replies

2. Shell Programming and Scripting

Getting date in seconds with decimals

I am trying to get date to display decimal Desired output 1350386096256.12 I know this can be done with printf, but are not able to make it work. I have tested this and many otherprintf "%.2f" $(($(date +%s%N)/1000000)) (8 Replies)
Discussion started by: Jotne
8 Replies

3. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

4. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

5. Shell Programming and Scripting

Decimals in TCSH

Hello, I want to run a loop with non-integer values (which I know I can't) so I've created a loop of integers and divided it by 10. However, these values are always rounded down to 1 significant figure. How do I get the script to keep and use the decimal value? My script is as follows #... (1 Reply)
Discussion started by: DFr0st
1 Replies

6. Shell Programming and Scripting

test expr VS [ expr ]

What is the difference between test expr VS . For example : if test 5 -eq 6 echo "Wrong" and if echo "Wrong" bot will give the same output as Wrong. Now, what is the difference between these two? though they are producing the same result why we need two? Any answer will be... (2 Replies)
Discussion started by: ashok.g
2 Replies

7. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

8. Shell Programming and Scripting

Bourne and decimals??

I need to get 15% of the variable exer1 to be added to other exercises so far, i've got exer1=$1 aver=`expr $exer \* .15` but i keep getting an error that an integer value was expected. Is there anyway around this? (1 Reply)
Discussion started by: kdyzsa
1 Replies

9. Shell Programming and Scripting

handle decimals

Hi All, How we can handle decimals in (Float) in UNIX. a=73 b=5 c=`expr a / b` i am getting 14 but i need full 14.6 . Can any one help me pls? (1 Reply)
Discussion started by: subin_bala
1 Replies

10. Shell Programming and Scripting

comparing two numbers with the decimals

Can someone tell me how do I comapre two numbers with the decimals in UNIX shell scripting I understand "-gt" can be used only for integers Regards, Giri (4 Replies)
Discussion started by: chittari
4 Replies
Login or Register to Ask a Question