M BUZZ CRAZE NEWS
// general

How to round decimals using bc in bash?

By Mia Morrison

A quick example of what I want using bash scripting:

#!/bin/bash
echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
echo "scale=2; $float/1.18" |bc -l
read -p "Press any key to continue..."
bash scriptname.sh

Assuming that the price is: 48.86 The answer will be:41.406779661 (41.40 actually because I'm using scale=2;)

My Question is:How I round the second decimal to show the answer in this way?: 41.41

3

14 Answers

Simplest solution:

printf %.2f $(echo "$float/1.18" | bc -l)
4

A bash round function:

round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};

Used in your code example:

#!/bin/bash
# the function "round()" was taken from
#
# the round function:
round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};
echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
#echo "scale=2; $float/1.18" |bc -l
echo $(round $float/1.18 2);
read -p "Press any key to continue..."

Good luck :o)

8

Bash/awk rounding:

echo "23.49" | awk '{printf("%d\n",$1 + 0.5)}' 

If you have python you can use something like this:

echo "4.678923" | python -c "print round(float(raw_input()))"
7

Here's a purely bc solution. Rounding rules: at +/- 0.5, round away from zero.

Put the scale you're looking for in $result_scale; your math should be where $MATH is located in the bc command list:

bc <<MATH
h=0
scale=0
/* the magnitude of the result scale */
t=(10 ^ $result_scale)
/* work with an extra digit */
scale=$result_scale + 1
/* your math into var: m */
m=($MATH)
/* rounding and output */
if (m < 0) h=-0.5
if (m > 0) h=0.5
a=(m * t + h)
scale=$result_scale
a / t
MATH
3

Here's an abbreviated version of your script, fixed to provide the output you want:

#!/bin/bash
float=48.86
echo "You asked for $float; This is the price without taxes:"
echo "scale=3; price=$float/1.18 +.005; scale=2; price/1 " | bc

Note that rounding up to nearest integer is equivalent to adding .5 and taking the floor, or rounding down (for positive numbers).

Also, the scale factor is applied at the time of operation; so (these are bc commands, you can paste them into your terminal):

float=48.86; rate=1.18;
scale=2; p2=float/rate
scale=3; p3=float/rate
scale=4; p4=float/rate
print "Compare: ",p2, " v ", p3, " v ", p4
Compare: 41.40 v 41.406 v 41.4067
# however, scale does not affect an entered value (nor addition)
scale=0
a=.005
9/10
0
9/10+a
.005
# let's try rounding
scale=2
p2+a
41.405
p3+a
41.411
(p2+a)/1
41.40
(p3+a)/1
41.41

Pure bc implementation as requested

define ceil(x) { auto os,xx;x=-x;os=scale;scale=0 xx=x/1;if(xx>x).=xx-- scale=os;return(-xx) }

if you put that in a file called functions.bc then you can round up with

echo 'ceil(3.1415)' | bc functions.bc

Code for the bc implementation found on

1

if you have the result, for instance consider 2.3747888

all you have to do is:

d=$(echo "(2.3747888+0.5)/1" | bc); echo $d

this rounds the number correctly example:

(2.49999 + 0.5)/1 = 2.99999 

the decimals are removed by bc and so it rounds down to 2 as it should have

#!/bin/bash
# - loosely based on the function "round()", taken from
#
# - inspired by user85321 @ askubuntu.com (original author)
# and Aquarius Power
# the round function (alternate approach):
round2()
{ v=$1 vorig=$v # if negative, negate value ... (( $(bc <<<"$v < 0") == 1 )) && v=$(bc <<<"$v * -1") r=$(bc <<<"scale=$3;(((10^$3)*$v/$2)+0.5)/(10^$3)") # ... however, since value was only negated to get correct rounding, we # have to add the minus sign again for the resulting value ... (( $(bc <<< "$vorig < 0") == 1 )) && r=$(bc <<< "$r * -1") env printf %.$3f $r
};
echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
round2 $float 1.18 2
echo && read -p "Press any key to continue..."

It is actually simple: there is no need to explicitly add a hardcoded "-0.5" variant for negative numbers. Mathematically spoken, we'll just compute the absolute value of the argument and still add 0.5 as we normally would. But since we (unfortunately) have no built-in abs() function at our disposal (unless we code one), we will simply negate the argument if it's negative.

Besides, it proved very cumbersome to work with the quotient as a parameter (since for my solution, I must be able to access the dividend and divisor separately). This is why my script has an additional third parameter.

3

I'm still looking for a pure bc answer to how to round just one value within a function, but here's a pure bash answer:

#!/bin/bash
echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
embiggen() { local int precision fraction="" if [ "$1" != "${1#*.}" ]; then # there is a decimal point fraction="${1#*.}" # just the digits after the dot fi int="${1%.*}" # the float as a truncated integer precision="${#fraction}" # the number of fractional digits echo $(( 10**10 * $int$fraction / 10**$precision ))
}
# round down if negative
if [ "$float" != "${float#-}" ] then round="-5000000000" else round="5000000000"
fi
# calculate rounded answer (sans decimal point)
answer=$(( ( `embiggen $float` * 100 + $round ) / `embiggen 1.18` ))
int=${answer%??} # the answer as a truncated integer
echo $int.${answer#$int} # reassemble with correct precision
read -p "Press any key to continue..."

Basically, this carefully extracts the decimals, multiplies everything by 100 billion (10¹⁰, 10**10 in bash), adjusts for precision and rounding, performs the actual division, divides back to the appropriate magnitude, and then reinserts the decimal.

Step-by-step:

The embiggen() function assigns the truncated integer form of its argument to $int and saves the numbers after the dot in $fraction. The number of fractional digits is noted in $precision. The math multiplies 10¹⁰ by the concatenation of $int and $fraction and then adjusts that to match the precision (e.g. embiggen 48.86 becomes 10¹⁰ × 4886 / 100 and returns 488600000000 which is 488,600,000,000).

We want a final precision of hundredths, so we multiply the first number by 100, add 5 for rounding purposes, and then divide the second number. This assignment of $answer leaves us at a hundred times the final answer.

Now we need to add the decimal point. We assign a new $int value to $answer excluding its final two digits, then echo it with a dot and the $answer excluding the $int value that's already taken care of. (Never mind the syntax highlighting bug that makes this appear like a comment)

(Bashism: exponentiation is not POSIX, so this is a bashism. A pure POSIX solution would require loops to add zeros rather than using powers of ten. Also, "embiggen" is a perfectly cromulant word.)


One of the main reasons I use zsh as my shell is that it supports floating point math. The solution to this question is quite straightforward in zsh:

printf %.2f $((float/1.18))

(I'd love to see somebody add a comment to this answer with the trick to enabling floating point arithmetic in bash, but I'm pretty sure that such a feature doesn't yet exist.)

I know it's an old question, but I have a pure 'bc'-solution without 'if' or branches:

#!/bin/sh
bcr()
{ echo "scale=$2+1;t=$1;scale-=1;(t*10^scale+((t>0)-(t<0))/2)/10^scale" | bc -l
}

Use it like bcr '2/3' 5 or bcr '0.666666' 2 --> (expression followed by scale)

That's possible because in bc (like C/C++) it's allowed to mixin logical expressions in your calculations. The expression ((t>0)-(t<0))/2) will evaluate to +/-0.5 depending on the sign of 't' and therefore use the right value for rounding.

1

I had to calculate the total duration of a collection of audio files.

So I had to:

A. obtain the duration for each file (not shown)

B. add up all the durations (they were each in NNN.NNNNNN (fp) seconds )

C. separate hours, minutes, seconds, subseconds.

D. output a string of HR:MIN:SEC:FRAMES, where frame = 1/75 sec.

(Frames come from SMPTE code used in studios.)


A: use ffprobe and parse duration line into a fp number (not shown)

B:

 # add them up as a series of strings separated by "+" and send it to bc
arr=( "${total[@]}" ) # copy array
# IFS is "Internal Field Separator"
# the * in arr[*] means "all of arr separated by IFS"
# must have been made for this
IFS='+' sum=$(echo "scale=3; ${arr[*]} "| bc -l)# (-l= libmath for fp)
echo $sum 

C:

# subtract each amount of time from tt and store it
tt=$sum # tt is a running var (fp)
hrs=$(echo "$tt / 3600" | bc)
tt=$(echo "$tt - ( $hrs * 3600 )" | bc )
min=$(echo "$tt / 60" | bc )
tt=$(echo "$tt - ($min *60)" | bc )
sec=$(echo "$tt/1" | bc )
tt=$(echo "$tt - $sec" | bc )
frames=$(echo "$tt * 75" | bc ) # 75 frames /sec
frames=$(echo "$frames/1" | bc ) # truncate to whole #

D:

#convert to proper format with printf (bash builtin)
hrs=$(printf "%02d\n" $hrs) # format 1 -> 01
min=$(printf "%02d\n" $min)
sec=$(printf "%02d\n" $sec)
frames=$(printf "%02d\n" $frames)
timecode="$hrs:$min:$sec:$frames"
# timecode "01:13:34:54"
bc() { while : do while IFS='$\n' read i do /usr/bin/bc <<< "scale=2; $i" | sed 's/^\./0&/' done done
}

You can use awk, no pipes necessary:

$> PRICE=48.86
$> awk -vprice=$PRICE "BEGIN{printf(\"%.2f\n\",price/1.18+0.005)}"
41.41

Set the price beforehand with the environment variable PRICE=<val>.

  • Then call the awk - $PRICE will go into awk as an awk variableprice.

  • It is then rounded up to the nearest 100th with +.005.

  • The printf formatting option %.2f limits the scale to two decimal places.

If you have to do this a lot this way is a lot more efficient than the selected answer:

time for a in {1..1000}; do echo $(round 48.86/1.18 2) > /dev/random; done
real 0m8.945s
user 0m6.067s
sys 0m4.277s
time for a in {1..1000}; do awk -vprice=$PRICE "BEGIN{printf(\"%.2f\n\",price/1.18+0.005)}">/dev/random; done
real 0m2.628s
user 0m1.462s
sys 0m1.275s

PRINTF NOT RELIABLE:

The printf method is very neat, easy to use & listed in lots of how to's.

However, if you have hard coded a decimal point, you should know that your code will probably fail if used one of the many countries that use a comma separator.

A surprising number of countries actually do use this & many of them in Europe. Probably as many as use a dot. According to Wikipedia > Decimal_separator the comma is noted as an ISO/IEC Directive ...

This is what happens if your computer has its number system set to use commas:

printf %.2f 573,5489
573,55

but

printf %.2f 573.5489
printf: 573.5489: invalid number

How do we get around this?

Using 'bc' is one way. On its own, simply with scale=n. It doesn't need to be piped as in some of the examples. Remember that scale only works on division and to divide your input sum by one. See the answer by @user525061

The other, especially if you have already code using the printf method, could be to change the locale of your shell session using 'export'.

Try:

export LC_NUMERIC=C

But then this negates the printf simplicity ....

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy