problem in awk int() function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in awk int() function
# 1  
Old 01-02-2009
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  
Old 01-02-2009
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  
Old 01-02-2009
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.

Last edited by Panos1962; 01-02-2009 at 10:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Function main returning int?

H friends, As we know, a function returns a value and that value is saved somwhere. like int Sum( int x, int y ) { return x + y; } Total = Sum( 10, 20 ); The value 30 is saved in variable Total. Now the question is, what int value does the function main return, and where is it... (5 Replies)
Discussion started by: gabam
5 Replies

2. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

3. Shell Programming and Scripting

Perl int function solved

Hello, I have the below perl function int to return the integer value from the expression but it is not. I am not sure if something misses out here. Any help on this? Thanks in advance. # Code sample Start my $size = int (`1134 sample_text_here`); print "$size \n"; # Code end ----------... (0 Replies)
Discussion started by: nmattam
0 Replies

4. Shell Programming and Scripting

Problem using function in awk

I created two functions that output two random variables. I want to output them in the output file. But it does not seem to work. # Function rgaussian1(r1, r2) # Gaussian random number generator function rgaussian1(r1, r2) { pi = 3.142 v1 = sqrt( -2 * log(rand()) ) v2... (18 Replies)
Discussion started by: kristinu
18 Replies

5. Shell Programming and Scripting

Awk issue using int function

Hi, I am having issue with awk command . This command is running in the command prompt but inside a shell script. awk -F'| ' 'int($1)==$1 && int($3) ==$3' int_check.txt $cat int_check.txt 123|abc|123x 234|def|345 When i run it inside a shell script i am getting the error "bailing... (5 Replies)
Discussion started by: ashwin3086
5 Replies

6. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

7. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

10. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies
Login or Register to Ask a Question