For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2007 > Newbie rounding question









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 Newbie rounding question
petervclark@googlemail.com

2007-02-28, 6:57 pm

I'm relatively new to AWK, in that I've used it for a couple of years,
but only infrequently, and using only a small number of functions. I
use AWK and GAWK in DOS, not Unix.

Here's my problem.

I use an array to sum a column of values indexed on an account code.

So I might have (where the first column is the Account Code and the
second column is a list of values)...
01 126
02 131
02 111
01 312

In my END section, I print the results of the array so they show as...
01 438
02 242

At the same time I print another value to the end of each line, which
is the value of the array multiplied by the value of a field in the
BEGIN section.

So in BEGIN, I have, for example...
UnitCost = 0.026

Then in END, I have, for example (assuming that the values 438 and 242
are the "InvCount" shown below)...
UnitCost = InvCount * UnitCost

The results I will get are 11.388 and 6.292 respectively.

What I need to do is round them up to the nearest whole number so that
I get 12 and 7 respectively. I ALWAYS need to round up.

Thanks in anticipation for any assistance.

loki harfagr

2007-03-01, 7:57 am

On Wed, 28 Feb 2007 11:52:24 -0800, petervclark wrote:

> I'm relatively new to AWK, in that I've used it for a couple of years,
> but only infrequently, and using only a small number of functions. I use
> AWK and GAWK in DOS, not Unix.
>
> Here's my problem.
>
> I use an array to sum a column of values indexed on an account code.
>
> So I might have (where the first column is the Account Code and the
> second column is a list of values)... 01 126
> 02 131
> 02 111
> 01 312
>
> In my END section, I print the results of the array so they show as...
> 01 438
> 02 242
>
> At the same time I print another value to the end of each line, which is
> the value of the array multiplied by the value of a field in the BEGIN
> section.
>
> So in BEGIN, I have, for example...
> UnitCost = 0.026
>
> Then in END, I have, for example (assuming that the values 438 and 242
> are the "InvCount" shown below)...
> UnitCost = InvCount * UnitCost
>
> The results I will get are 11.388 and 6.292 respectively.
>
> What I need to do is round them up to the nearest whole number so that I
> get 12 and 7 respectively. I ALWAYS need to round up.
>
> Thanks in anticipation for any assistance.


May the following help you :-)

$ cat numbers.awk
function rounded(x)
{
return (x == int(x)) ? x : int(x+(x<0?-.5:.5) )
}
function roundup(x)
{
return (x == int(x)) ? x : int(x+(x<0?-1:1))
}
{
print "_"
for(i=1;i<=NF;i++){
print "-"
print "Rounded "$i" == "rounded($i)
print "Roundup "$i" == "roundup($i)
}
}

$ echo "-11.555 -11.214 -11 0 11 11.214 11.5555 " | awk -f numbers.awk
_
-
Rounded -11.555 == -12
Roundup -11.555 == -12
-
Rounded -11.214 == -11
Roundup -11.214 == -12
-
Rounded -11 == -11
Roundup -11 == -11
-
Rounded 0 == 0
Roundup 0 == 0
-
Rounded 11 == 11
Roundup 11 == 11
-
Rounded 11.214 == 11
Roundup 11.214 == 12
-
Rounded 11.5555 == 12
Roundup 11.5555 == 12
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com