21

I have two numbers:

value1=686
value2=228.35

I am not able to add an integer and a float. Please help me out to get the result.

I am running it in bash.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
sasuke
  • 595
  • 1
  • 5
  • 15

4 Answers4

24
echo 1 + 3.5 | bc

awk "BEGIN {print 1+3.5; exit}"

python -c "print 1+3.5"

perl -e "print 1+3.5"

Just replace the numbers with your variables, eg: echo $n1 + $n2 | bc

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 2
    thnx :) bc language is not present on my linux machine and i dont have rights to install it asd well that was big problem. – sasuke May 03 '13 at 09:55
10

If you have the bc language installed, you can do the following:

#!bin/bash
numone=1.234
numtwo=0.124
total=`echo $numone + $numtwo | bc`
echo $total

If you don't have bc, then you can try with awk. Just in one single line:

echo 1.234 2.345 | awk '{print $1 + $2}'

There are plenty of other options, also. Like python, perl, php....

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
5

Bash doesn't have floating-point types, but you can use a calculator such as bc:

a=686
b=228.35
c=`echo $a + $b | bc`
echo "$c"
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
tapas dey
  • 51
  • 1
  • 1
-7
 #!/bin/Bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
fedorqui
  • 275,237
  • 103
  • 548
  • 598
snehal
  • 1,798
  • 4
  • 17
  • 24