The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mail function problem jwholey Shell Programming and Scripting 4 05-01-2008 12:21 PM
problem, with if condition in function gurukottur Shell Programming and Scripting 4 04-24-2008 03:57 PM
PERL function problem avadhani Shell Programming and Scripting 2 06-15-2005 05:18 AM
Problem with the strlen function in ksh steiner Shell Programming and Scripting 3 07-24-2003 09:39 AM
rexec() function problem lcmoreno High Level Programming 7 01-08-2002 10:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-02-2009
qa.bingo qa.bingo is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 1
problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor '
BEGIN {
printf("wgt:" wgt "\n");
printf("factor:" fac "\n");
total = sprintf("%.0f", wgt * fac);
total2 = sprintf("%.0f", int(wgt * fac));
printf("total:" total "\n");
printf("total2:" total2 "\n");
}
'
if
vWeight=326.4
vFactor=100

the result would be:
total:32640
total2:32639

Could anyone know how is 32639 calucated?

thanks!
  #2 (permalink)  
Old 01-02-2009
JerryHone JerryHone is offline
Registered User
  
 

Join Date: Nov 2006
Location: UK
Posts: 178
Rounding

Rounding!

Try this...

awk 'BEGIN {printf("%.50f\n", 326.4*100)}'

and you should see something like...

32639.99999999999636202119290828704833984375000000000000

Including the int function simply rounds the output down to 32639. 'int' does not round to the nearest integer

printf (or sprintf) rounds by default when using %f, so without the int, it will display 32640.

HTH.

Jerry
  #3 (permalink)  
Old 01-02-2009
Panos1962 Panos1962 is offline
Registered User
  
 

Join Date: Dec 2008
Location: Thessaloniki, GREECE
Posts: 29
Rounding numbers in awk

I send you an attached file with the awk source of a simple function for rounding numbers in any scale. Of course there are some limits, but you can edit the code and make it better (if you can).
Have fun.
Attached Files
File Type: txt round.txt (438 Bytes, 49 views)

Last edited by Panos1962; 01-02-2009 at 10:04 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0