Totals in a file - incorrectly displaying

Afternoon,

I have a script which creates/modifies data into a formatted csv.

The trailer record should display 2 columns, the first is a static entry of "T" to identify it as a trailer record. The 2nd is a total of amounts in a column throughout the entire file.

My total isn't displaying correctly. I do not use decimal points and the following code is not displaying the final digit. It displays every other digit correctly (just missing the 2nd digit after the decimal point)

At the minute I have

 $(awk '{s+=$3}END {print "T, "s} 

On a seperate note, considering it is the 3rd column I am totalling, the header record has 3 columns which I do not want included - it seems to work just now ever though the header has a fixed number in the 3rd column (does awk count the $'s from 0 or from 1?)

Thanks

it counts from $1 ... $NF

Use printf("T,%15.4\n", s)

15 = size of field, .4 = number characters after decimal, the precision.

When you ask questions like this examples are much better: like expected output. The answer I just gave you could be wrong....

Ah ok...actual output =

  
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 499

Expect output =

  
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 4999

As you can see the trailer record should hold both decimal places, but it doesn't. Considering the file is a csv, the field length is variable - the only thing I can confirm is the last 2 digits in the column will be the 2 digits on the right of the decimal place.

I appreciate your help

given your sample input:

$ awk -F, '{s+=$3}1;END {print "T ",s}' OFS=, myFile
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 96928

What's wrong with this output?

AH, I have remember something.

I add the header record myself near the end of the script, by the time I have done this I have already created the trailer record - meaning the total is complete and doesn't use the header.

Anymore thoughts as to why my code is not correctly using 2 decimal places?

Hmmm...... I don't quite understand your question and/or reference to the '2 decimal places'.
Can you maybe rephrase it as I don't see any 'decimal places' - you're dealing with integers.
Does the alternate solution solve your issue?

sorry, the 3rd column in my sample is basically an amount field. I have removed the decimal places for other reasons. Anyway, as it is an amount field the the final 2 digits are the 2 on the right of the decimal place.

Even so, my problem is the code I am using is not correctly displaying the total, it is missing the right hand digit.
$(awk '{s+=$3}END

Hmmm..... so what you're saying is that you used to have floats values in the 3-rd field. Then you converted all 3-rd field values to integers (removed the fractions). And now you're expecting the float values for the trailer/sum row?
How's that possible?

I must be uber confused. Maybe other could understand the dilemma better....

I am going to try and be a bit clearer. I use the following code to display the total of all fields in a column - $(awk '{s+=$3}END {print "T, "s}'. This prints "T, Total"

Sample file:

C, 818128, 5612.13122010,,,,
C, 818129, 1598.13122010,,,,
C, 818130, 7896.13122010,,,,
C, 818131, 2655.13122010,,,,
T, 1776

I am hoping it displays 17761...can anyone explain why it doesnt?

---------- Post updated at 08:28 AM ---------- Previous update was at 05:59 AM ----------

Got it sorted...

It was down to decimal points which I have been removing, for some reason when i removed the decimal point it removed the final digit.

Reorganisation and step by step breakdown solved this problem - many thanks