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 rows below highlighted in red are added, I only get 2 decimal places 18.34).

awk -F "|" '{a[$2$3$4$5$6$7]+=$8;}END{for(i in a)print i"|"a;}' File1  > File2  
echo File2

|ACCT1 | XX | 00548  |02/13/2018 |548JN4 |L |18.424000
|ACCT2 | XY | 00551  |02/13/2018 |540JC7 |L |315.809000
|ACCT3 | XX | 00639  |02/13/2018 |368024 |L |4.000000
|ACCT4 | XX | 00652  |02/13/2018 |546QD4 |L |35.212000
|ACCT5 | XY | 00663  |02/13/2018 |5210E1 |L |16.242000
|ACCT5 | XY | 00663  |02/13/2018 |5210E1 |L |2.102000

Welcome to the forum.

For your problem, try

awk -F "|" '{a[$2$3$4$5$6$7]+=$8;}END{for(i in a)print i"|"a;}' CONVFMT="%.6f" file1
ACCT1  XX  00548  02/13/2018 548JN4 L |18.424000
ACCT2  XY  00551  02/13/2018 540JC7 L |315.809000
ACCT3  XX  00639  02/13/2018 368024 L |4
ACCT5  XY  00663  02/13/2018 5210E1 L |18.344000
ACCT4  XX  00652  02/13/2018 546QD4 L |35.212000
1 Like
awk -F "|" '{a[$2$3$4$5$6$7]+=$8;}END{for(i in a)printf "%s|%.6f\n",  i, a}' File1
1 Like

Thank you for your help. this really works in terms of adding.

But, I have a new problem where if it is all zeroes after the decimal, then it truncated.

for example, if the value is 4.000000, then the result I get is 4.

But I want the output to be 4.000000

use printf instead:

awk -F "|" '{a[$2$3$4$5$6$7]+=$8;}END{for(i in a)printf("%s%s%.6f\n",  i, OFS, a;}' OFS='|' file1