Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

rounding off in awk
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


Report this thread to moderator Post Follow-up to this message
Old Post
sudhakar.sankar@gmail.com
05-25-05 01:55 PM


Re: rounding off in awk

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-25-05 08:55 PM


Re: rounding off in awk
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);


Report this thread to moderator Post Follow-up to this message
Old Post
fjchia
05-25-05 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.