How to sum up two decimal values?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sum up two decimal values?
# 1  
Old 10-01-2010
How to sum up two decimal values?

I am running the following script :
Code:
cat ind_sls_extr_UX.out_sorted | while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
  final_amount=0
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`expr ${amount} + ${amount}`
  fi
done

The value amount is fetching a decimal value, but in final amount the value is not getting summed up :

Code:
00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

How to resolve it.

Moderator's Comments:
Mod Comment Use code tags to display code, data, logs etc. please, ty.
# 2  
Old 10-01-2010
Error

Pls search the forum before firing your question.

try using bc at the end

Code:
final_amount=`expr ${amount} + ${amount} |bc`

# 3  
Old 10-01-2010
thanks for the reply,I have searched...

but, didn't get the expected result...now the result is summing up values upto one level only
Result :
Code:
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48


The file looks like...

Code:
45RA0290837AMERISOURCEBERGEN DRUG                  9129364353+00046        09-24-101149372000U01000023360026224100262241 76225
2011 50458 58801 0914101128414+0000001            7474 S CAMINO DTUCSON    AZ85746    00000
              +00037024.84+00454.74+00149.66+00000305.08+00000305.08                 PASCUA YAQUI TRIBE ARIZON20100924N1C

45RA0289062AMERISOURCEBERGEN DRUG                  9129364356+00087        09-24-101148687000U010004340600157238AK782486300860
802  50458 58801 0914101127749+0000002            20940 BURBANK BWOODLAND HCA91367660100000
              +00075924.20+00454.74+00386.53+00000136.42+00000136.42                 KAISER FND HOSP PHARM 4352010092401C

60RA0290988AMERISOURCEBERGEN DRUG                  9129364361+00057        09-24-101149373000U010002053200011438AK767668000860
802  50458 58801 0913101128415+0000002            10400 E ALAMEDADENVER    CO80231    00000
              +00032679.10+00454.74+00386.53+00000136.42+00000136.42                 KAISER FDN HLTH PLN OF CO2010092401C

45RA0290736AMERISOURCEBERGEN DRUG                  9129364366+00093        09-24-101149374000U010004665600344756BH611129176225
2011 50458 58801 0914101128416+0000001            6100 N HAGGERTYCANTON    MI48187368300000
              +00067329.69+00454.74+00149.66+00000305.08+00000305.08                 HENRY FRD MEDCL CTR PHARM20100924Y1C

45RA0289000AMERISOURCEBERGEN DRUG                  9129366455+00184        09-24-101148684000U01000614890000782000007820 76170
1481 50458 58801 0902101127747-0000012            47149 BUSE RD  PATUXENT RMD20670154000000
              +00237192.08+00454.74+00132.04-00003872.40-00003872.40                 NAVAL MEDCL CLINIC       2010092405D

60RA0289000AMERISOURCEBERGEN DRUG                  9129366455+00184        09-24-101148684000U010006148900055120AU467502776170
1481 50458 58801 0915101127747+0000006            73 NEALY BLVD  LANGLEY AFVA23665202300000
              +00237192.08+00454.74+00132.04+00001936.20+00001936.20                 1 MDG SGSL               2010092401C

45RA0289000AMERISOURCEBERGEN DRUG                  9129366455+00184        09-24-101148684000U010006148900110626BS700513476716
331  50458 58801 0915101127747+0000003            6501 N CHARLES TOWSON    MD21204681900000
              +00237192.08+00454.74+00454.51+00000000.69+00000000.69                 SHEPPARD PRATT HLTH SYS  2010092401C

45RA0316958AMERISOURCEBERGEN DRUG                  9129371835+00372        09-24-101148692000U010041354800008136BD224763676170
1481 50458 58801 0916101127758+0000048            36000 DARNALL LFORT HOOD TX76544    00000
              +00247836.02+00454.74+00132.04+00015489.60+00015489.60                 DARNALL ARMY COMM HOSP   2010092401C

60RA0316958AMERISOURCEBERGEN DRUG                  9129371835+00372        09-24-101148692000U010041354800008149AW533330376170
1481 50458 58801 0916101127758+0000007            2200 BERGQUIST SAN ANTONITX78236990700000
              +00247836.02+00454.74+00132.04+00002258.90+00002258.90                 WHMC/MSLS (FM3047)       2010092401C

60RA0316958AMERISOURCEBERGEN DRUG                  9129371835+00372        09-24-101148692000U010041354800248892BM572513876225
2011 50458 58801 0916101127758+0000001            400 W 4TH ST   ODESSA    TX79761504500000
              +00247836.02+00454.74+00149.66+00000305.08+00000305.08                 MEDICAL CTR HOSP OUTPATIE20100924Y1C


Last edited by Franklin52; 10-01-2010 at 10:05 AM.. Reason: Please use code tags
# 4  
Old 10-01-2010
Looks like a little logic error to me.
The variable ${final_amount} is being zeroised on every iteration.
We should move to to outside the loop.
The arithmetic line is faulty in logic (adding the wrong variable) and syntax (expr can't deal with decimal places). You presumably want to add ${amount} to ${final_amount} on each iteration.
Also, without knowing what Shell you are using it is hard to be sure whether the updated variable is available outside the loop.

Code:
final_amount=0
cat ind_sls_extr_UX.out_sorted | while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`echo "${final_amount} + ${amount}"|bc`
  fi
done


Last edited by methyl; 10-01-2010 at 09:23 AM.. Reason: layout & include Michaelrozar17's maths idea - rewritten and corrected
# 5  
Old 10-01-2010
@methyl still getting the same....the shell is ksh

changed code :
Code:
while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`echo "${amount} + ${amount}" | bc`
echo  ${final_amount}
else
echo "next"
fi
done < ind_sls_extr_UX.out_sorted

result getting :

Code:
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
next
next


Last edited by Franklin52; 10-01-2010 at 10:07 AM.. Reason: Please use code tags
# 6  
Old 10-01-2010
Sorry, you may have been too quick while I was editing and re-editing the post. I realised that michaelroxar17's correction didn't actually work and went off to test an alternative (which is in the corrected post).

Note that we are incrementing the running ${final_amount} rather than adding ${amount} to ${amount} which just doubled the value!

Code:
final_amount=`echo "${final_amount} + ${amount}"|bc`

This User Gave Thanks to methyl For This Post:
# 7  
Old 10-04-2010
@methayl...thanks a lot...

It worked..actually I've seen ur post after I implemented the same....thanks a lot....SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

3. Shell Programming and Scripting

How compare decimal values?

I have 2 files say tp1.txt and tp2.txt having following data cat tp1.txt abc,2.20,IN20 acb,3.15,DN10 bca,3,RD10 cat tp2.txt alv,1.00,IN20 aaa,4.05,DD10 abb,5.50,RD12 i want to compare the values on 2nd field of both the file, if value of first tp1.txt is greater than value... (3 Replies)
Discussion started by: ranabhavish
3 Replies

4. Shell Programming and Scripting

Rounding off decimal values

Hi Friends, This is my last post for today. My input file is chr1 100 200 chr1 123 300 chr1 300 400 chr1 420 520 chr10 132344343 132348674 When I try using this command awk '{v=($3+$2)/2; print $0"\t"v}' 1 This is my output chr1 100 200 150 chr1 123 300 211.5 (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

6. Shell Programming and Scripting

Sum of decimal numbers in column

Hi!!! I have n decimal numbers in column: 1.23 3.45 5.16 . . . How to do arithmetic sum of theese numbers??? Thanks!!!:D (4 Replies)
Discussion started by: tdev457
4 Replies

7. UNIX for Dummies Questions & Answers

Sum up a decimal column in a tab separated text file and error handling

Hi, I have a small requirement where i need to sum up a column in a text file. Input file 66ab 000000 534385 -00000106350.00 66cd 000000 534485 -00013364511.00 66ad 000000 534485 -00000426548.00 672a 000000 534485 000000650339.82... (5 Replies)
Discussion started by: pssandeep
5 Replies

8. Shell Programming and Scripting

print decimal values

Hi guys I'm trying to print average of 2 columns. awk '{print ($1+$2)/2}' file.txt Its printing average but not giving decimal values its giving 3.05521e+08 instead of 305521.... I tried %f to print float values but not quiet connected Could you help plz:confused: (5 Replies)
Discussion started by: repinementer
5 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question