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:

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

Try this...

printf("%10f", x)

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

--ahamed

printf("sum: %d\n", x)

thanks for the reply. i tried the both and here is the outcome:

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:

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.

printf("%.2f\n", x)

BTW, the $ is part of your prompt!

--ahamed

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.

Well, you never asked for any until now. Try %.2f.

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?

something like

awk -v ORS="+" '{ print $1 } END { printf("0.00\n"); }' file | bc

which should print a+b+c+d+e+f+g+... z+0.00, feed that into bc, which will sum them up with arbitrary precision.