Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk is rounding numeric variables, any way around it?

Status
Not open for further replies.

cgoodman

Programmer
Dec 29, 2008
7
Hey,

I am having an issue with awk rounding numeric variables.

My script is as follows:

Code:
function ControlSumSix()
{
   while((getline < MYFILENAME)>0) {
    COUNT =  $CFIELD; 
    COUNT2 = $CFIELD2; 
    COUNT3 = $CFIELD3; 
    COUNT4 = $CFIELD4; 
    COUNT5 = $CFIELD5; 
    COUNT6 = $CFIELD6; 
    
    SUM  += COUNT;
    SUM2 += COUNT2;
    SUM3 += COUNT3;
    SUM4 += COUNT4;
    SUM5 += COUNT5;
    SUM6 += COUNT6;
   } 
   close(MYFILENAME) 
}

The code sums up the numeric values found in specified columns (CFIELDs are hard coded into program). I am checking that the sum (SUM1, SUM2...) from the function is equal to the actual sum of the column. However, awk is rounding the values of this variables. For one file, the actual sum of the column was 128034.35, while the program computed the sum to be 128034.4.

Obviously, there is a rounding issue; however, the real question is - Is there any way around this error and why is awk rounding the numeric value when the number isn't that big?

Thanks for all help.

Chris
 
Can we see the sample data or is there too much of it?

How are you printing the result?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top