bash math...can't divide!!
I try to make a division like so:
VAR1=$(( 28331 / 33009)); echo "Result is: $VAR1"; It should come out with "Result is 6.05"....but no, it comes with: $ Result is 0 When I do it with smaller numbers like 100 / 3 it works perfectly. What is the problem? |
Quote:
The reason you're getting 0 is that the answer is given as a truncated integer. |
use awk
Code:
awk 'BEGIN{print 28331 / 33009}' |
Code:
mherring@Ath:~$ echo $((100/3)) There is a utility for bash that does real decimal math, but I don't remember what it is. This is easier: Code:
python |
Interesting. I didn't know awk could do math. I'll have to remember that one myself.
|
Quote:
|
Quote:
|
Maybe easier than gawk, you can invoke "bc" to do floating point arithmetic
|
Code:
echo "4 k 100 3 / p" | dc Code:
answer=$(echo "4 k 100 3 / p" | dc) You guessed it, "4 k" determines the number of decimal places. jlinkels |
This worked actually, thanks!
echo "scale=5; (54321 / 12345)" | bc |
All times are GMT -5. The time now is 03:27 PM. |