add numbers with decimal place in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add numbers with decimal place in UNIX
# 1  
Old 07-21-2005
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?
# 2  
Old 07-21-2005
Use bc

Code:
echo "1.1+1" | bc

gives

2.1

Check out the man page for bc

vino
# 3  
Old 07-21-2005
dc is another choice. it's up to you.
Code:
dc -e '1.1 1+p'

# 4  
Old 07-21-2005
thanks! do you know how to round the result to the nearest whole number?
# 5  
Old 07-21-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zero padding a Number before and after a decimal place

Hi I was hoping someone could help me with a sed script I am trying to write? I am on a Mac running ElCapitan I have some text that I have converted from a pdf that I want to format into an xml file. In the file I have managed to delete all the text I do not need. The text I have left is... (8 Replies)
Discussion started by: Paul Walker
8 Replies

2. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

3. UNIX for Dummies Questions & Answers

How to trim the decimal place for all the columns?

Dear all, I have a file call test.txt which has 2000 columns, 1000 rows. If I want to trim all the columns to 3 decimal places, how can I do it? I know how to use awk prinf to trim specic columns. But I don't know how to trim all the columns. Thank you. data sample: 0.976004565 9.34567845... (6 Replies)
Discussion started by: forevertl
6 Replies

4. Shell Programming and Scripting

Using awk to place decimal points at proper position

Hi, I have one input file which is delimited by pipe. I want to put decimal points in this input file at particular position in particular column and also get the negative sign (if any) at start of that column. $ cat Input_file.txt 11|10102693|1|20151202|10263204|20151127|N|0001... (7 Replies)
Discussion started by: Prathmesh
7 Replies

5. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

6. Shell Programming and Scripting

Unable to get the Specific column with 2 decimal place

Hi, I have an issue converting decimal places of a particular column, i am using below script to get the output, but the output is not generating in desired format. awk -F"," 'BEGIN{OFS=","}{if(NR==0)getline;if ($7 != "") {if ($7 > 0) $7=$7/100 ; {printf "%.2f"... (3 Replies)
Discussion started by: rramkrishnas
3 Replies

7. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

8. Shell Programming and Scripting

Regarding decimal numbers

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be... (7 Replies)
Discussion started by: ravindra22
7 Replies

9. Shell Programming and Scripting

Comparing Decimal Numbers

Im trying to compare two numbers with decimals but its not working as expected. a=1 b=1.1 if then echo "equal" fi When I do this it says that the numbers are equal. Ultimately Im using -le and -ge in the if statements but I tested with -eq for simplicity. Any way to make this... (3 Replies)
Discussion started by: Grizzly
3 Replies

10. Shell Programming and Scripting

decimal numbers

Hi friends How can I use "for loop" for decimal numbers? ex: 0.1 < x < 0.6 I used this commands but does'nt work. LIMIT=0.6 for ((x=0.1; x<=LIMIT; x++)) do - - - done Many thanks (1 Reply)
Discussion started by: snow
1 Replies
Login or Register to Ask a Question