Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding of a Value 1

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

Has anyone got an idea on how to round off a value for e.g
A hex value of 0007c5 is == 1989 (198.9) and this should be rounded off as 199

newnum=tolower(substr($6,1,6))
newnum7=tolower(substr($5,1,6))
split("31 28 31 30 31 30 31 31 30 31 30 31",m)
"date +'%Y'" | getline yy
if (yy%4 == 0) m[2] = 29
dd=$2
mm = 1
while (dd > m[mm]) {
dd -= m[mm]
mm++
}
AN=substr($1,1,15);gsub("F","",AN)
BN=substr($4,1,15);gsub("F","",BN)
DA=tolower(substr($3,1,6))
TM=$3
HH=substr($3,1,2)
MM=substr($3,3,2)
SS=substr($3,5,2)
AN="0"AN
slen = length(newnum)
slen7 = length(newnum7)

decnum = 0
for (i=1;i<slen+1;i++)
{
tpos = index(hexstr,substr(newnum,i,1))-1
decnum=decnum+((16**(slen -i))*tpos)
}

decnum7 = 0
for (i=1;i<slen7+1;i++)
{
tposOP = index(hexstr,substr(newnum7,i,1))-1
decnum7=decnum7+((16**(slen7 -i))*tposOP)

}
printf ("%15s %4.4d/%2.2d/%2.2d %s:%s:%s %15s %10s %10s\n",AN,yy,mm,dd,HH,MM,SS,BN,decnum,decnum7

Many Thanks
Chris
 
Something like this ?
d7=(decnum7+5)"";decnum7=substr(d7,1,length(d7)-1)


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Nice to see you back in action.

The results look incorrect for e.g

Hex 0000A3 == 163 and you are rounding it to "2". Should be 16.3 , rounded off to 16

decnum7 = 0
for (i=1;i<slen7+1;i++)
{
tposOP = index(hexstr,substr(newnum7,i,1))-1
decnum7=decnum7+((16**(slen7 -i))*tposOP)
d7=(decnum7+5)""
decnum7=substr(d7,1,length(d7)-1)

Am I overlooking the problem.

Once Again
Many Thanks
Chris
 
Sorry for the typo:
d7=(10*decnum7+5)"";decnum7=substr(d7,1,length(d7)-1)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
HI PHV,

Still incorrect. This time the result is 163, nothing has changed.

Thanks
Chris
 
This works for me:
decnum7 = 0
for (i=1;i<slen7+1;i++) {
tposOP = index(hexstr,substr(newnum7,i,1))-1
decnum7=decnum7+((16**(slen7 -i))*tposOP)
}
d7=(decnum7+5)"";decnum7=substr(d7,1,length(d7)-1)


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Great move.

Please explain:

d7=(decnum7+5). Why 5? (5 places !!!!)
and why the "" at the end.


Many Thanks
Chris
 
d7=(decnum7+5)"";decnum7=substr(d7,1,length(d7)-1)
The basic idea is to get the rounded value of decnum7/10 (1989 -> 199, 163 -> 16)
The classical way to round a value is to get the integral part of (value + 0.5)
As awk does all calculations in floating point and lacks an int function, I do the truncating with string manipulation:
d7=(decnum7+5)""
Add 5 to decnum7 giving d7 and coerce d7 to string
decnum7=substr(d7,1,length(d7)-1)
Suppress the last digit of d7, simulating an integral division by 10, and put the result back to decnum7.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Great explantion.
Much appreciated

Till we meet again.

Thanks
Chris
 
Urm PHV, awk doesn't lack an int function. I believe it was even in the original "old" awk.
 
BTW, you're right Ygor ![blush]
So, frangac, a simpler way to do the trick:
decnum7=int((decnum7+5)/10)


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top