Round a Divided Number in Bash

Round a divided number in Bash

To do rounding up in truncating arithmetic, simply add (denom-1) to the numerator.

Example, rounding down:

N/2
M/5
K/16

Example, rounding up:

(N+1)/2
(M+4)/5
(K+15)/16

To do round-to-nearest, add (denom/2) to the numerator (halves will round up):

(N+1)/2
(M+2)/5
(K+8)/16

Rounding up float point numbers bash

In case input contains a number, there is no need for an external command like bc. You can just use printf:

printf "%.3f\n" "$input"

Edit: In case the input is a formula, you should however use bc as in one of the following commands:

printf "%.3f\n" $(bc -l <<< "$input")
printf "%.3f\n" $(echo "$input" | bc -l)

In bash how do I divide two variables and output the answer rounded upto 5 decimal digits?

The problem here is that you missed the echo (or printf or any other thing) to provide the data to bc:

$ echo "scale=5; 12/7" | bc
1.71428

Also, as noted by cnicutar in comments, you need to use $ to refer to the variables. sum is a string, $sum is the value of the variable sum.

All together, your snippet should be like:

sum=12
n=7
output=$(echo "scale=5;$sum/$n" | bc)
echo "$output"

This returns 1.71428.

Otherwise, with "scale=5;sum/n"|bc you are just piping an assignment and makes bc fail:

$ "scale=5;sum/n"|bc
bash: scale=5;sum/n: No such file or directory

You then say that you want to have the result rounded, which does not happen right now:

$ sum=3345699
$ n=1000000
$ echo "scale=5;($sum/$n)" | bc
3.34569

This needs a different approach, since bc does not round. You can use printf together with %.Xf to round to X decimal numbers, which does:

$ printf "%.5f" "$(echo "scale=10;$sum/$n" | bc)"
3.34570

See I give it a big scale, so that then printf has decimals numbers enough to round properly.

Bash, need to count variables, round to 2 and store to variable

Yes, it is working! I did it like this.

Arithmetic operations:

pricevat=$(echo "$vat * ${array[5]}" + ${array[5]} | bc -l)

Round to 3 places:

pricevat=$(printf "%0.3f\n" $pricevat)

If there is another way to do it better or together on one line, let me know please.

Thanks.

Bash: How to do decimal number division?

kent$  num=12.53

kent$ echo "scale=2;$num/5"|bc
2.50

kent$ awk -v n="$num" 'BEGIN{printf "%.2f\n", n/5}'
2.51

note the bc's scale and printf's format may give different result.

How do I use floating-point arithmetic in bash?

You can't. bash only does integers; you must delegate to a tool such as bc.

How to round to 1 decimal in bash?

This is not so much a bash question as a bc one, but bc doesn’t round. The easiest approach is probably just to add .05 after the division, then set scale to 1 and divide by 1 to force truncation.

You can do that by using a variable inside bc to hold the unrounded result:

promedio=$(bc <<<"scale=2; p=$lineas/($dias*24); scale=1; (p+0.05)/1")

Or you could do it all in one go by throwing in an extra multiplication by 10, rounding at that magnitude, and then dividing back down:

promedio=$(bc <<<"scale=1; (10*$lineas/($dias*24)+0.5)/10")

Divide two variables in bash

shell parsing is useful only for integer division:

var1=8
var2=4
echo $((var1 / var2))

output:
2

instead your example:

var1=3
var2=4
echo $((var1 / var2))

ouput:
0

it's better to use bc:

echo "scale=2 ; $var1 / $var2" | bc

output:
.75

scale is the precision required

How to round a floating point number upto 3 digits after decimal point in bash

What about

a=`echo "5+50*3/20 + (19*2)/7" | bc -l`
a_rounded=`printf "%.3f" $a`
echo "a = $a"
echo "a_rounded = $a_rounded"

which outputs

a         = 17.92857142857142857142
a_rounded = 17.929

?



Related Topics



Leave a reply



Submit