# Float array sum

**URL:** <https://community.unix.com/t/float-array-sum/338195>\
**Category:** Shell Programming and Scripting\
**Created:** [December 30, 2013, 10:31am UTC](https://community.unix.com/t/float-array-sum/338195 "2013-12-30T10:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![berveglieri](https://community.unix.com/letter_avatar/berveglieri/32/6_449c533f586939b6e5f246632a46421f.png) [@berveglieri](https://community.unix.com/u/berveglieri)\
**Post date:** [December 30, 2013, 10:31am UTC](https://community.unix.com/t/float-array-sum/338195/1 "2013-12-30T10:31:45Z")

</div>

Hi everyone,

I'm having some trouble with float array.  
When i try to get the array sum with float numbers i get this error line 39: soma + 2.34 | bc: syntax error: invalid arithmetic operator (error token is ".34 | bc")

```nohighlight
 26 Somar() {
 27 echo "Quantos numeros deseja somar?"
 28 read numeros
 29 vetorSoma[$numeros]=" "
 30 for (( i=0;i<$numeros;i++ ))
 31 do
 32 con=$((con+1))
 33 echo -n "digite o $con� numero: "  
 34 read vetorSoma[$i]
 35 done
 36 
 37 for i in ${vetorSoma[@]}; do
 38 sum=0
 39 soma=$((soma + $i | bc))
 40 done
 41 echo "O resultado foi $soma"
 42 }

```

when i type int numbers it works great, i just don't know why.  
Thx.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [December 30, 2013, 11:23am UTC](https://community.unix.com/t/float-array-sum/338195/2 "2013-12-30T11:23:22Z")

</div>

You need to `printf / echo` a string including your numbers and the arithmetic operation (+ possibly other `bc` commands like "scale") and pipe that to `bc` .

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [December 30, 2013, 11:35am UTC](https://community.unix.com/t/float-array-sum/338195/3 "2013-12-30T11:35:35Z")

</div>

> [@berveglieri](#):
>
> Hi everyone,
> 
> I'm having some trouble with float array.  
> When i try to get the array sum with float numbers i get this error line 39: soma + 2.34 | bc: syntax error: invalid arithmetic operator (error token is ".34 | bc")
> 
> ```plaintext
> 26 Somar() {
> 27 echo "Quantos numeros deseja somar?"
> 28 read numeros
> 29 vetorSoma[$numeros]=" "
> 30 for (( i=0;i<$numeros;i++ ))
> 31 do
> 32 con=$((con+1))
> 33 echo -n "digite o $con� numero: "  
> 34 read vetorSoma[$i]
> 35 done
> 36 
> 37 for i in ${vetorSoma[@]}; do
> 38 sum=0
> 39 soma=$((soma + $i | bc))
> 40 done
> 41 echo "O resultado foi $soma"
> 42 }
> 
> ```
> 
> when i type int numbers it works great, i just don't know why.  
> Thx.

Your variable soma is not set initially, that will through some error, along with usage of bc as RudiC noticed.

Try :

```plaintext
Somar() { 
echo "Quantos numeros deseja somar?" 
read numeros 
vetorSoma[$numeros]=" " 
for (( i=0;i<$numeros;i++ )) 
do 
con=$((con+1)) 
echo -n "digite o $con� numero: " 
read vetorSoma[$i] 
done 

soma=0 
for i in ${vetorSoma[@]}; do 
soma=$(echo "scale=2;$soma + $i" | bc -l) 
done 
echo "O resultado foi $soma" 
} 

# call Somar
Somar

```

---

<div class="post-metadata">

**Author:** ![berveglieri](https://community.unix.com/letter_avatar/berveglieri/32/6_449c533f586939b6e5f246632a46421f.png) [@berveglieri](https://community.unix.com/u/berveglieri)\
**Post date:** [December 30, 2013, 12:19pm UTC](https://community.unix.com/t/float-array-sum/338195/4 "2013-12-30T12:19:52Z")

</div>

Thx Mr RudiC and Mr Akshay Hegde.

Now i can store the float values and saw where i went wrong.

Thx.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [December 30, 2013, 5:18pm UTC](https://community.unix.com/t/float-array-sum/338195/5 "2013-12-30T17:18:33Z")

</div>

Note that if you use a 1993 or later ksh, you can just use something like:

```nohighlight
soma=0
for i in ${vetorSoma[@]}; do
        soma=$((soma + $i))
done
printf "O resultado foi %.2f\n" "$soma"

```

Obviously, adjust the number in red above to set the number of digits you want displayed after the decimal point in your results.
