Awk Floating Point Number Addition Results Are Unexpected

Posted on in Categories , , , last updated July 29, 2013

I am using awk to grep ‘foo’ from a text file and cacluate sum of field # 7. But, result is rounded to an integer. I need exact result such as 385858.66 and not 385858 using the following command:

grep ‘foo’ 2012-2013.txt | awk ‘BEGIN{ sum=0.0}{ sub(“,”,””,$7); sum +=$7}END{ print “$” sum}’

I want $682444.57 as output. How can I force “awk” to do floating point math?