The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2005
Registered User
 

Join Date: May 2005
Posts: 40
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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-21-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,662
Use bc

Code:
echo "1.1+1" | bc
gives

2.1

Check out the man page for bc

vino
Reply With Quote
  #3 (permalink)  
Old 07-21-2005
r2007's Avatar
Registered User
 

Join Date: Jun 2005
Location: China
Posts: 73
dc is another choice. it's up to you.
Code:
dc -e '1.1 1+p'
Reply With Quote
  #4 (permalink)  
Old 07-21-2005
Registered User
 

Join Date: May 2005
Posts: 40
thanks! do you know how to round the result to the nearest whole number?
Reply With Quote
  #5 (permalink)  
Old 07-21-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,102
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0