Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
    
I find it weird because "printf "%0.2f\n" 41.445" does now work but "printf "%0.2f\n" 41.435 and printf "%0.2f\n" 41.455" do. Even your own case works (On 12.04) but not with the .445 –  Luis Alvarado Aug 24 '12 at 16:44

3 Answers 3

up vote 10 down vote accepted

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 
# http://stempell.com/2009/08/rechnen-in-bash/

# 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)

share|improve this answer
1  
Right now, this is the nicest way to do it, thanks for your knowledge. –  blackedx Aug 24 '12 at 16:51
    
btw, if the number is negative, we have to use -0.5 –  Aquarius Power Jun 4 at 4:47
    
Couldn't get the example to work on the testing console! "Debugging" a little revealed that for e. g. $2 = 3 I had to use echo $(env printf %.3f $(echo "scale=3;((1000*$1)+0.5)/1000" | bc)). Mind the env before printf! This'll teach ya again that it's always important to understand what you're copy-pasting from elsewhere. –  syntaxerror Oct 23 at 23:04
    
@Aquarius Power thanks for the inspiration! I've now forked a version of the above script which will work with both negative and positive numbers. –  syntaxerror Oct 23 at 23:48

Pure bash 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()))"

share|improve this answer
    
Thanks for the tip but it doesn't solve what I need. I have to do it just by using bash... –  blackedx Aug 24 '12 at 14:39
    
The python command is more readable and good for quick scripts. Also supports arbitrary digit rounding by adding e.g. ", 3" to the round function. Thanks –  Jordan Trudgett Nov 13 '13 at 2:07
#!/bin/bash
# - loosely based on the function "round()", taken from 
# http://stempell.com/2009/08/rechnen-in-bash/

# - inspired by user85321 @ askubuntu.com (original author)
#   and Aquarius Power

# the round function (alternate approach):

round2()
{
v=$1
vorig=$v
# if negative, negate value ...
[[ $(echo "$v < 0" | bc) -eq 1 ]] && v=$(echo "$v * -1" | bc)
r=$(echo "scale=$3;(((10^$3)*$v/$2)+0.5)/(10^$3)" | bc)

# ... however, since value was only negated to get correct rounding, we 
# have to add the minus sign again for the resulting value ...

[[ $(echo "$vorig < 0" | bc) -eq 1 ]] && r=$(echo "$r * -1" | bc)
echo $(env printf %.$3f $r)
};

echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
echo $(round2 $float 1.18 2);
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.