Computing.Net > Forums > Unix > Rounding Problem in awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Rounding Problem in awk

Reply to Message Icon

Name: sankarjune14
Date: April 26, 2005 at 06:27:15 Pacific
OS: UNIX
CPU/Ram: RAM: 256 MB
Comment:

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.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 >> "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



Sponsored Link
Ads by Google

Response Number 1
Name: thepubba
Date: April 26, 2005 at 06:39:42 Pacific
Reply:

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=3

print "scale=2; $A/$B" | /usr/bin/bc

This will return 3.33


0

Response Number 2
Name: sankarjune14
Date: May 4, 2005 at 22:34:53 Pacific
Reply:

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


0

Response Number 3
Name: boudhayan
Date: May 5, 2005 at 08:07:29 Pacific
Reply:

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}'
3

HTH.
Boudhayan.


0

Response Number 4
Name: sankarjune14
Date: May 5, 2005 at 22:48:27 Pacific
Reply:

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.89

But, I am expecting 2856.90

Let me know if you have any ideas on this.

Thanks.


Sudhakar


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Rounding Problem in awk

sorting command problem in awk www.computing.net/answers/unix/sorting-command-problem-in-awk-/3552.html

korn shell script problem (using awk ? ) www.computing.net/answers/unix/korn-shell-script-problem-using-awk-/3547.html

URGENT! help with sorting in awk www.computing.net/answers/unix/urgent-help-with-sorting-in-awk/4442.html