Home > Archive > AWK > May 2005 > rounding off in awk
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
rounding off in awk
|
|
| sudhakar.sankar@gmail.com 2005-05-25, 8:55 am |
| I am using the following awk script:
BEGIN {
OFMT = "%10.4f";
}
END {
print "Test Starting";
l1 = 675.5
l2 = 306.9
l3 = 613.8
l4 = 135.1
l5 = 82.5
l6 = 135.1
l7 = 400
l8 = 82.5
t = 17.5;
t1 = l1 * t / 100;
t2 = l2 * t / 100;
t3 = l3 * t / 100;
t4 = l4 * t / 100;
t5 = l5 * t / 100;
t6 = l6 * t / 100;
t7 = l7 * t / 100;
t8 = l8 * t / 100;
print "t1 = " t1;
print "t2 = " t2;
print "t3 = " t3;
print "t4 = " t4;
print "t5 = " t5;
print "t6 = " t6;
print "t7 = " t7;
print "t8 = " t8;
total = 0;
total = total + l1 + t1;
print "total 1 = " total;
total = total + l2 + t2;
print "total 2 = " total;
total = total + l3 + t3;
print "total 3 = " total;
total = total + l4 + t4;
print "total 4 = " total;
total = total + l5 + t5;
print "total 5 = " total;
total = total + l6 + t6;
print "total 6 = " total;
total = total + l7 + t7;
print "total 7 = " total;
total = total + l8 + t8;
print "total 8 = " total;
printf("%10.2f\n", total);
}
The output will be:
Test Starting
t1 = 118.2125
t2 = 53.7075
t3 = 107.4150
t4 = 23.6425
t5 = 14.4375
t6 = 23.6425
t7 = 70
t8 = 14.4375
total 1 = 793.7125
total 2 = 1154.3200
total 3 = 1875.5350
total 4 = 2034.2775
total 5 = 2131.2150
total 6 = 2289.9575
total 7 = 2759.9575
total 8 = 2856.8950
2856.89
But, I need 2856.90 (two decimal ROUND off). Is there any other way to
do this? I want to do this calculation in awk script only.
Thanks in advance
Sudhakar
| |
| Ed Morton 2005-05-25, 3:55 pm |
|
sudhakar.sankar@gmail.com wrote:
> I am using the following awk script:
> BEGIN {
> OFMT = "%10.4f";
> }
>
> END {
> print "Test Starting";
> l1 = 675.5
> l2 = 306.9
> l3 = 613.8
> l4 = 135.1
> l5 = 82.5
> l6 = 135.1
> l7 = 400
> l8 = 82.5
>
> t = 17.5;
>
> t1 = l1 * t / 100;
> t2 = l2 * t / 100;
> t3 = l3 * t / 100;
> t4 = l4 * t / 100;
> t5 = l5 * t / 100;
> t6 = l6 * t / 100;
> t7 = l7 * t / 100;
> t8 = l8 * t / 100;
>
> print "t1 = " t1;
> print "t2 = " t2;
> print "t3 = " t3;
> print "t4 = " t4;
> print "t5 = " t5;
> print "t6 = " t6;
> print "t7 = " t7;
> print "t8 = " t8;
>
> total = 0;
> total = total + l1 + t1;
> print "total 1 = " total;
> total = total + l2 + t2;
> print "total 2 = " total;
> total = total + l3 + t3;
> print "total 3 = " total;
> total = total + l4 + t4;
> print "total 4 = " total;
> total = total + l5 + t5;
> print "total 5 = " total;
> total = total + l6 + t6;
> print "total 6 = " total;
> total = total + l7 + t7;
> print "total 7 = " total;
> total = total + l8 + t8;
> print "total 8 = " total;
> printf("%10.2f\n", total);
> }
>
> The output will be:
> Test Starting
> t1 = 118.2125
> t2 = 53.7075
> t3 = 107.4150
> t4 = 23.6425
> t5 = 14.4375
> t6 = 23.6425
> t7 = 70
> t8 = 14.4375
> total 1 = 793.7125
> total 2 = 1154.3200
> total 3 = 1875.5350
> total 4 = 2034.2775
> total 5 = 2131.2150
> total 6 = 2289.9575
> total 7 = 2759.9575
> total 8 = 2856.8950
> 2856.89
>
> But, I need 2856.90 (two decimal ROUND off). Is there any other way to
> do this? I want to do this calculation in awk script only.
Take a look at the "round()" function at
http://www.gnu.org/software/gawk/ma...#Round-Function
Regards,
Ed.
| |
| fjchia 2005-05-25, 3:55 pm |
| sudhakar.sankar@gmail.com wrote:
> I am using the following awk script:
> BEGIN {
> OFMT = "%10.4f";
> }
>
> END {
> print "Test Starting";
> l1 = 675.5
> l2 = 306.9
> l3 = 613.8
> l4 = 135.1
> l5 = 82.5
> l6 = 135.1
> l7 = 400
> l8 = 82.5
>
> t = 17.5;
>
> t1 = l1 * t / 100;
> t2 = l2 * t / 100;
> t3 = l3 * t / 100;
> t4 = l4 * t / 100;
> t5 = l5 * t / 100;
> t6 = l6 * t / 100;
> t7 = l7 * t / 100;
> t8 = l8 * t / 100;
>
> print "t1 = " t1;
> print "t2 = " t2;
> print "t3 = " t3;
> print "t4 = " t4;
> print "t5 = " t5;
> print "t6 = " t6;
> print "t7 = " t7;
> print "t8 = " t8;
>
> total = 0;
> total = total + l1 + t1;
> print "total 1 = " total;
> total = total + l2 + t2;
> print "total 2 = " total;
> total = total + l3 + t3;
> print "total 3 = " total;
> total = total + l4 + t4;
> print "total 4 = " total;
> total = total + l5 + t5;
> print "total 5 = " total;
> total = total + l6 + t6;
> print "total 6 = " total;
> total = total + l7 + t7;
> print "total 7 = " total;
> total = total + l8 + t8;
> print "total 8 = " total;
> printf("%10.2f\n", total);
> }
>
> The output will be:
> Test Starting
> t1 = 118.2125
> t2 = 53.7075
> t3 = 107.4150
> t4 = 23.6425
> t5 = 14.4375
> t6 = 23.6425
> t7 = 70
> t8 = 14.4375
> total 1 = 793.7125
> total 2 = 1154.3200
> total 3 = 1875.5350
> total 4 = 2034.2775
> total 5 = 2131.2150
> total 6 = 2289.9575
> total 7 = 2759.9575
> total 8 = 2856.8950
> 2856.89
>
> But, I need 2856.90 (two decimal ROUND off). Is there any other way to
> do this? I want to do this calculation in awk script only.
>
> Thanks in advance
> Sudhakar
Maybe this will work....
..
..
print "total 8 = " total;
total=total+0.005
printf("%10.2f\n", total);
|
|
|
|
|