There is a subcommand "scale" in bc, which sets the digits after the decimal point. I once built a ksh-function around that, which I hereby donate to the community:
Code:
# ------------------------------------------------------------------------------
# f_Round rounding numbers
# ------------------------------------------------------------------------------
# Author.....: Wolf Machowitsch
# last update: 1999 03 08 by: Wolf Machowitsch
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99 1999 03 08 Original Creation
# -
#
# - 1.00 1999 03 24 Production Release
# minor Code refinements, debugging
#
# ------------------------------------------------------------------------------
# Usage:
# f_Round num parm1 [ int digits ]
#
# Example: f_Round 3.1415926 3 # rounds to 3 digits, giving 3.142
# f_Round $var # default for digits is zero, $var
# # is rounded to an int
#
#
# Prerequisites:
# - to use this function, the FPATH variable must be set
#
# - functional dependencies: f_CheckNumeric()
# f_CheckInteger()
#
# ------------------------------------------------------------------------------
# Documentation:
# f_Round() takes the first (integer) parameter and rounds it to the
# number of digits AFTER the decimal point given in $2. If no $2 is
# supplied the default value of 0 is assumed and $1 is rounded to an
# integer.
# The rounding is performed using the common algorithm of adding 5 to
# the digit to the right of the digit to round and then truncating the
# digits right to this.
#
# Parameters: num parm1 a char representing a number
# int digits an integer representing the numbers of
# decimals to remain after rounding
#
# returns: 0: no error
# 1: type error, parm1 not a number or $2 not an int
# 2: internal error, no parameter supplied
#
# ------------------------------------------------------------------------------
# known bugs:
#
# - it is not possible to round to digits before the decimal point
#
# ------------------------------------------------------------------------------
# ......................(C) 99 Wolf Machowitsch ................................
# ------------------------------------------------------------------------------
f_Round ()
{
$chFullDebug
# internal variables
typeset -i iRetVal=0 # return value (see docu)
typeset nValue="$1" # value to round
typeset -i iDigits="$2" # number of digits
typeset -i iDigit2=0 # for the rounding
typeset nAdd="0." # for the rounding
typeset -i iCnt=0 # general counter
if [ -z $iDigits ] ; then # set default for iDigits
iDigits=0
fi
if [ $# -lt 1 ] ; then # parameter check
iRetVal=2
else
if [ $(f_CheckNumeric $nValue; print $?) -gt 0 ] ; then
iRetVal=1
fi
if [ $(f_CheckInteger $iDigits; print $?) -gt 0 ] ; then
iRetVal=1
fi
fi
if [ $iRetVal -gt 0 ] ; then # exit on param. error
return $iRetVal
fi
iDigit2=$((( iDigits+1 )))
(( iCnt=0 ))
while [ $iCnt -lt $iDigits ] ; do
nAdd=$(print "$nAdd"0)
(( iCnt += 1 ))
done
nAdd=$(print "$nAdd"5)
# calculate rounded value
nValue=$( print "scale=$iDigits; $( print "scale=$iDigit2; $nValue + $nAdd" |\
bc \
) / 1" | \
bc \
)
print "$nValue"
return $iRetVal
}
# --- EOF f_Round
Hope this helps.
bakunin