Truncate or Roundoff a calculation

Hi friends,

I have a file of this kind

chr1	3180929	3181465	48
chr1	3196182	3196586	26
chr1	3343498	3343814	25
chr1	3351126	3351383	16

I did some calculations on the 4th column using the following command.

chr1	3180929	3181465	48	2.94665e-061000000
chr1	3196182	3196586	26	1.5961e-061000000
chr1	3343498	3343814	25	1.53471e-061000000
chr1	3351126	3351383	16	9.82217e-071000000
chr1	3410973	3411501	97	5.95469e-061000000

I would like to truncate my fifth column to 5 digits after the decimal. But, I want that output directly from the calculation not after printing the 5th column and truncating digits in the output file.

This is because, if we do the truncation after calculation, the value after e- is different and might yield different results.

All thoughts appreciated.

[edit] maybe not. Thinking.

1 Like
{ v=sprintf("%f", v); split(v, a, "e"); v=a[1]; print $0"\t"v; }
1 Like

Thanks for your solution, Corona!

It print 0.00000 in the 5th column.

 awk '{(v=($4/16289685)10^6); {split(v, a, "e"); v=a[1]; print $0"\t"v}}'
1 Like