problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor '
BEGIN {
printf("wgt:" wgt "\n");
printf("factor:" fac "\n");
total = sprintf("%.0f", wgt * fac);
total2 = sprintf("%.0f", int(wgt * fac));
printf("total:" total "\n");
printf("total2:" total2 "\n");
}
'
if
vWeight=326.4
vFactor=100

the result would be:
total:32640
total2:32639

Could anyone know how is 32639 calucated?

thanks!

Rounding!

Try this...

awk 'BEGIN {printf("%.50f\n", 326.4*100)}'

and you should see something like...

32639.99999999999636202119290828704833984375000000000000

Including the int function simply rounds the output down to 32639. 'int' does not round to the nearest integer

printf (or sprintf) rounds by default when using %f, so without the int, it will display 32640.

HTH.

Jerry

I send you an attached file with the awk source of a simple function for rounding numbers in any scale. Of course there are some limits, but you can edit the code and make it better (if you can).
Have fun.