Using Fractional Numbers in Shell Variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Fractional Numbers in Shell Variables
# 1  
Old 04-19-2012
Using Fractional Numbers in Shell Variables

Hello everyone,

Before I state my problem I would like to inform you that this is not an assignment. I'm just trying to learn some shell programming in my free time.

I'm trying to write a parking fee calculator. But I don't seem to be able to use fractional numbers. Could you please advice me on how to proceed?

Code:
#while duration is greater than 1
while [[ $DURATION -gt 1 ]]
do
	FEE=$((FEE + 2)) #I can't seem to make add fractional numbers 
	DURATION=$((DURATION-1))
done

Thank you in advance. Smilie
# 2  
Old 04-19-2012
I assume you want to use numbers like 4.11, .84. There are some shells that directly support arithmetic operations on decimal numbers, like zsh.

Otherwise you have to use command line tools like bc.
Examples:
Code:
sum=$(echo ".3 + 1.12" | bc -l)
a=.13
b=.01
diff=$( echo "$a - $b" | bc -l)

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-19-2012
And ksh93
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-19-2012
Hi I did as you instructed but it doesn't work, it gives me a syntax error: invalid arithmetic operator (error token is ""1+1.5" | bc -l")


Code:
do
	FEE=$((echo "$FEE + 1.5" | bc -l)) 
	DURATION=$((DURATION-1))
done

Smilie
# 5  
Old 04-19-2012
Hi, you need to use single parentheses, not double..
# 6  
Old 04-19-2012
Just for the fun of it, here's an example of adding floats in ksh93 on Solaris. Maybe it will help:
Code:
$ cat float_example.ksh
#!/usr/dt/bin/dtksh

typeset -F a=2.3  # Declare variables as floats.
typeset -F b=1.2
typeset -F result

result=$( (($a + $b)) )

printf "a=%4.2f\n" $a
printf "b=%4.2f\n" $b
printf "a+b=%4.2f\n" $result

exit 0

$ float_example.ksh
a=2.30
b=1.20
a+b=3.50
$

# 7  
Old 04-19-2012
$(( )) is used for arithmetic. You want $( ) which is used for command substitution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing multiple variables containing numbers

a=1 456 b=4928 c=23 d=456 I want to compare four variables to get the name of the variable having the highest number (2 Replies)
Discussion started by: proactiveaditya
2 Replies

2. Shell Programming and Scripting

Help about using variables of float numbers in sed

Hi, I need to run a Fortran program which reads a input file with a fixed name many times, each time I need to change a number (real) in that input file, this is how I currently do it and I know it is not elegent at all: cp inputfile.dat backup.dat sed -i 's/28.0/0.01/g' inputfile.dat ./myCode... (3 Replies)
Discussion started by: dypang
3 Replies

3. Shell Programming and Scripting

Processing text variables that have numbers in CShell

Hello, I have a case where I need to process an environment variable text value in CShell to calculate the product of 2 numbers stated in the variable string having the format "<first_number>Letter x<second_number>". Ex: $VAR = "24x20" I need to set $VAR_PRODUCT = "Product of 20 and 24"... (3 Replies)
Discussion started by: mohy
3 Replies

4. Shell Programming and Scripting

using sed command to display contents where line numbers are stored in variables

if i want to display the contents of a file between say line number 3 and 10 then i use the following command sed -n '3,10p' filename if this 3 was contained in x and 10 was contained in y then how wud this command modified? sed -n '$x,$yp' filename does not work..please advise (2 Replies)
Discussion started by: arindamlive
2 Replies

5. Shell Programming and Scripting

PERL - rounding fractional number

It seems that perl sprintf uses the round-to-even method: foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) { printf "$i -> %.0f\n", $i; } __END__ 0.5 -> 0 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 4 Where we probably wants to use round-half-up, i.e. output should be as below: 0.5 -> 1 1.5 -> 2... (8 Replies)
Discussion started by: ganapati
8 Replies

6. Shell Programming and Scripting

[Doubt] How can I store numbers less than 1? Shell variables

Hi everyone, I am having some problems with my scripts so I hope you could help me. I am trying to store the result of a division in a variable in tcshell but I have the problem that if: For example, dividing 2/100 the result is 0.02 but if I store that I have "0". How can I have 0.02... (8 Replies)
Discussion started by: Bloody
8 Replies

7. Shell Programming and Scripting

Reading numbers thru shell

Dear pal, I want to read only numbers thru a shell script, If user is trying to enter character it should block that ... Pls help to me find out a solution (2 Replies)
Discussion started by: bhagyaraj.p
2 Replies

8. Shell Programming and Scripting

Can i use Variables in sed command in line numbers

I wish to give line number from one point to another in sed command like this sed -n 1,1000000p file1.txt >file2.txt but variable line number $x,$x+100000 can i give it cos i tried and it was giving an error any suggestions?/ Thx in advance AC (2 Replies)
Discussion started by: bezudar
2 Replies

9. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies

10. Shell Programming and Scripting

add numbers in shell script

cat dailyreports | grep "Important list" | awk -F":" '{print $2}' | awk -F" " '{print $1}' hey guys, after running the above combination of cat and awk, i get the below output: 3 4 2 9 now, i need to add these numbers up all in one line. i dont know what to add to that cat and awk one... (2 Replies)
Discussion started by: Terrible
2 Replies
Login or Register to Ask a Question