![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Round off decimal number to 2 decimal places? | Vozx | Shell Programming and Scripting | 1 | 12-07-2005 06:53 PM |
| Devision of Decimal Numbers? | Vozx | Shell Programming and Scripting | 4 | 12-07-2005 02:26 AM |
| compare decimal numbers | tmxps | Shell Programming and Scripting | 5 | 11-02-2005 02:46 PM |
| unix script for converting a decimal to binary | softy | Shell Programming and Scripting | 3 | 10-19-2005 06:33 AM |
| best place for unix drivers ? | jimmyok | UNIX for Dummies Questions & Answers | 1 | 02-04-2002 05:45 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
add numbers with decimal place in UNIX
i am trying to add numbers with decimal place and I am prompted with an error.
this expr command works :add=`expr 1 + 1` :echo $add :2 but when i am trying to add :addThis=`expr 1.1 + 1` :expr: An integer value was expected. is there a way to add numbers with decimal place in UNIX? |
| Forum Sponsor | ||
|
|
|
|||
|
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
bakunin |
|||
| Google The UNIX and Linux Forums |