sum in exponential form


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sum in exponential form
# 1  
Old 01-26-2012
sum in exponential form

Hello guys,

i have got a flat file and it has got balance amount value as one of its fields. my mission is to add the total of those balance values. i am getting that but the result is coming up in exponential form, which is not the right way. i want to see the sum as a normal decimal number but not in expenential number. any help would be appriciated.

the code i have got is:

Code:
cut -d"|" -f10 /bx201a/pwchome0001/pcdev/server/TgtFiles/standard_csv_file1.out | awk '{ x += $1 } END { print "Sum: " x }'

the output is:
Sum: 1.02678e+09

regards
jd sony
# 2  
Old 01-26-2012
Try this...
Code:
printf("%10f", x)

Without real data, it is difficult for us to guess...

--ahamed
# 3  
Old 01-26-2012
printf("sum: %d\n", x)
# 4  
Old 01-26-2012
thanks for the reply. i tried the both and here is the outcome:
Code:
printf("%10f", x)

i am getting something like this:
1026780823.850019bash-2.03$
two problems here.
i don't know why that $ prompt is getting attached to the value.
second problem is, the original balance has got values upto only two decimals. but the outcome has got values upto 6 decimals. i don't know where the extra 4 decimal values are coming from.

with the other code, which is:
Code:
printf("sum: %d\n", x)

i am not getting any decimal values at all. this is the output:
sum: 1026780823

the file is a huge file with millions of records in it. i can give you some sample values for that column if that helps

202.62
0.00
4678.41
etc. its a column of (9,2) precission and scale in oracle terms.so it has got 2 digits after the decimal point and 7 digits before the decimal point. further help would be appriciated.
# 5  
Old 01-26-2012
Code:
printf("%.2f\n", x)

BTW, the $ is part of your prompt!

--ahamed
# 6  
Old 01-26-2012
Quote:
Originally Posted by jdsony
i don't know why that $ prompt is getting attached to the value. second problem is, the original balance has got values upto only two decimals. but the outcome has got values upto 6 decimals. i don't know where the extra 4 decimal values are coming from.
awk uses floating point numbers, it doesn't have infinite precision. It's stored internally as a value like x * 2^n. If you want infinite precision down to the exact last decimal point, awk is the wrong tool.
Quote:
i am not getting any decimal values at all.
Well, you never asked for any until now. Try %.2f.
# 7  
Old 01-26-2012
fantasitc. that works out ahamed. thanks for your time and adivse.

regards

---------- Post updated at 06:25 PM ---------- Previous update was at 06:21 PM ----------

Hi Corona688,

thanks for the explanation. so what do you suggest instead of awk if i need good precision?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk-Exponential Values

Hi Friends, My input Gene1 4.14887050399078e-49 Gene2 5.39999891278828e-10 Gene 2.22108326729483e-11 How do I change the above exponential values to normal values? Thanks (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom. This works, but like awk only cat file | head -n-y | awk 'NR>(x-1)' so remove last 3 lines and 5 firstcat file | head -n-3 | awk 'NR>4' (5 Replies)
Discussion started by: Jotne
5 Replies

3. Shell Programming and Scripting

AWK - Avoid exponential value

I'm using the following command, but how can I avoid printing exponential value (highlighted):- awk ' BEGIN { OFS=FS="|" } { if(NF>4) $10=int(((3.77*$11)/100 + $11)); } { print } ' infile CR|20121022|105|GSM|N|SAN|00122|SAN|75082|6.03929e+06|5819880|5794769|25111... (7 Replies)
Discussion started by: Yoda
7 Replies

4. Shell Programming and Scripting

Transpose Data form Different form

HI Guys, I have data in File A.txt RL03 RL03_A_1 RL03_B_1 RL03_C_1 RL03 -119.8 -119.5 -119.5 RL07 RL07_A_1 RL07_B_1 RL07_C_1 RL07 -119.3 -119.5 -119.5 RL15 RL15_A_1 RL15_C_1 RL15 -120.5 -119.4 RL16... (2 Replies)
Discussion started by: asavaliya
2 Replies

5. Shell Programming and Scripting

Convertion from Exponential to Decimal

I am trying to read values from excel and perform some calculations but I am getting below error: expr 2.326227180240883E7 / 8.509366417956961E8 expr: non-numeric argument Can anyone let me know how do i convert thse exponential numbers to decimal. (2 Replies)
Discussion started by: sachinnayyar
2 Replies

6. Shell Programming and Scripting

Exponential issues

I have the below awk command to search a row and find the line number. It returns the value in exponential. I understand we can use the print "%.0f\n" to convert the exponential. I wanted to have this in my awk command. Can anyone advise bnd=`awk '/^GS/{p=NR}$0~"^ABC.*\\*"k{f=1}/^GE/&&f{print... (3 Replies)
Discussion started by: Muthuraj K
3 Replies

7. Shell Programming and Scripting

exponential format

awk -F"\t" -vv1=$name 'BEGIN{OFS="\t"} {gsub ("http://www."v1".com","",$6);print $1"\t-\t-","",$6,$7,$9,$2,$14,$15"\t""Apache=-\t-",$8*1000000"\t-\t-\t-\tdeflate=-\trmt=-"}' file.txt > a.txt file.txt 144.130.7.153 www.chi.com - 18/Jul/2010:00:00:00 +0000 GET... (3 Replies)
Discussion started by: sandy1028
3 Replies

8. Shell Programming and Scripting

Convert exponential value to decimal

Hi, Here is my script to read a file into array: awk -F '+' ' # load first file into array indexed by fields 1 and 2 NR == FNR { file1nr = FNR for (i=3; i<NF; i++) { file1 = $i } I have this... (5 Replies)
Discussion started by: Sangtha
5 Replies

9. UNIX for Advanced & Expert Users

Changing Unix form to Microsoft Word form to be able to email it to someone.

Please someone I need information on how to change a Unix form/document into a microsoft word document in order to be emailed to another company. Please help ASAP. Thankyou :confused: (8 Replies)
Discussion started by: Cheraunm
8 Replies
Login or Register to Ask a Question