Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am using the following awk script:
BEGIN {
OFMT = "%10.4f";
}END {
print "Test Starting" > "testfile";
l1 = 675.5
l2 = 306.9
l3 = 613.8
l4 = 135.1
l5 = 82.5
l6 = 135.1
l7 = 400
l8 = 82.5t = 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 >> "testfile";
print "t2 = " t2 >> "testfile";
print "t3 = " t3 >> "testfile";
print "t4 = " t4 >> "testfile";
print "t5 = " t5 >> "testfile";
print "t6 = " t6 >> "testfile";
print "t7 = " t7 >> "testfile";
print "t8 = " t8 >> "testfile";total = 0;
total = total + l1 + t1;
print "total 1 = " total >> "testfile";
total = total + l2 + t2;
print "total 2 = " total >> "testfile";
total = total + l3 + t3;
print "total 3 = " total >> "testfile";
total = total + l4 + t4;
print "total 4 = " total >> "testfile";
total = total + l5 + t5;
print "total 5 = " total >> "testfile";
total = total + l6 + t6;
print "total 6 = " total >> "testfile";
total = total + l7 + t7;
print "total 7 = " total >> "testfile";
total = total + l8 + t8;
print "total 8 = " total >> "testfile";}
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?Thanks in advance
Sudhakar

You can use bc. Here is an example:
#!/bin/ksh
print "scale=2; 33/1000" | /usr/bin/bc
This will return .03
So, if
A=10
B=3print "scale=2; $A/$B" | /usr/bin/bc
This will return 3.33

Here i am getting 3.33
If I'm using the A=20, it is giving 6.66 . But, I need 6.67
And, I need to do this calculation in awk script itself.
Let me know if you have any ideas on that.
Thanks.
Sudhakar

Sudhakar,
Try this -
$>echo | awk '{s = 8 / 3; printf "%5.5f\n", s}'
2.66667$>echo | awk '{s = 8 / 3; printf "%5.3f\n", s}'
2.667$>echo | awk '{s = 8 / 3; printf "%5.1f\n", s}'
2.7$>echo | awk '{s = 8 / 3; printf "%5.0f\n", s}'
3HTH.
Boudhayan.

Thanks Buodhayan.
But, in my case i am getting result as 2856.8950. If you use %5.2f, it is giving the following result:
$> echo | awk '{s =2856.8950; printf "%5.2f\n", s}'
2856.89But, I am expecting 2856.90
Let me know if you have any ideas on this.
Thanks.
Sudhakar

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |