awk calculation automatically rounding off the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk calculation automatically rounding off the output
# 1  
Old 08-07-2012
awk calculation automatically rounding off the output

I have some calculation in my script which is similar to the below example . I find that sometimes when using large decimal digits, the output gets automatically rounded off and it is affecting the program. I am not able to understand what is happening here..

Code:
awk '{ 
a=6.32498922
a1=6.324
b=52
c=12.65
 
d=(a*b/c)
d1=(a1*b/c)
 
printf(" d = %s;",d)
printf(" d1 = %s\n",d1)
 
result1=d*300
result2=d1*300
 
printf(" result1=> %s\n",result1)
printf(" result2=> %s\n",result2)
}' test1

prints:
Code:
 d = 26; d1 = 25.9959
result1=> 7799.99
result2=> 7798.77

Where is the decimals points in "d" . And if it got rounded off, why is the decimals appearing in "result1"

Could you help me understand what really is happening ? How can i make sure that the decimals does not get rounded off automatically?

Last edited by zaxxon; 08-07-2012 at 08:39 AM.. Reason: code tags
# 2  
Old 08-07-2012
Use f instead of d in printf which stands for "floating point".
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-07-2012
Set CONVFMT to a proper value:
Code:
awk 'BEGIN { 
CONVFMT="%-20.6f"
a=6.32498922
a1=6.324
b=52
c=12.65
 
d=(a*b/c)
d1=(a1*b/c)
 
printf(" d = %s;",d)
printf(" d1 = %s\n",d1)
 
result1=d*300
result2=d1*300
 
printf(" result1=> %s\n",result1)
printf(" result2=> %s\n",result2)
}'

which gives
Code:
 d = 25.999956           ; d1 = 25.995889
 result1=> 7799.986706
 result2=> 7798.766798

# 4  
Old 08-07-2012
But this will give .000000 for an integer result .. That again creates issues... Smilie . For whole numbers, I do not want the "." to appear in the variable value
# 5  
Old 08-07-2012
It will not.
Code:
awk 'BEGIN { 
CONVFMT="%-20.6f"
a=19
b=20.5
printf ("a = %s\n",a)
printf ("b = %s\n",b)
}'

a = 19
b = 20.500000

Integers get converted to strings as integers, irrespective of the value of CONVFMT (or OFMT).

Last edited by elixir_sinari; 08-07-2012 at 11:18 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 08-08-2012
i dont know.. maybe due to some other built-in vars, but i am getting it this way:

Code:
> awk 'BEGIN {
> CONVFMT="%-20.6f"
> a=30
> b=1.5
> printf ("a = %s\n",a)
> printf ("b = %s\n",b)
> c=a/b
> printf ("c = %s\n",c)
> }'

a = 30.000000
b = 1.500000
c = 20.000000


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-08-2012 at 11:43 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk calculation with zero as N/A

In the below awk, I am trying to calculate percent for a given id. It is very close the problem is when the # being used in the calculation is zero. I am not sure how to code this condition into the awk as it happens frequently. The portion in italics was an attempt but that lead to an error. Thank... (13 Replies)
Discussion started by: cmccabe
13 Replies

2. Shell Programming and Scripting

awk calculation wrong field output

The awk below is close but I can't seem to fix it to produce the desired output. Thank you :). current awk with output awk '{c1++; c2+=($2)} END{for (e in c1) print e, c1, c2}' input EFCAB5 2 50 USH2A 2 19 desired... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

[awk] rounding a float number?

Heyas Trying to calculate the total size of a file by reading its bitrate. Code snippet: fs_expected() { # # Returns the expected filesize in bytes # pr_str() { ff=$(cat $TMP.info) d="${ff#*bitrate: }" echo "${d%%,*}" | $AWK '{print $1}' | head -n 1 } t_BYTERATE=$((... (9 Replies)
Discussion started by: sea
9 Replies

5. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

6. Shell Programming and Scripting

awk, floating point and rounding

I had a person bring an interesting problem to me that appears to involve some sort of rounding inside awk. I've verified this with awk and nawk on Solaris as well as with gawk 3.1.5 on a Linux box. The original code fragment he brought me was thus: for (index=0; index < 1; index=index+.1) ... (4 Replies)
Discussion started by: mmyer2
4 Replies

7. Shell Programming and Scripting

AWK rounding up numbers

Hi, I have managed to round up numbers by using the following command: echo "5.54" | awk '{printf "%.0f\n", $1}' result 6 How can I round up all the numbers in a column in a file and print the lines with the new calculated totals? Thanks, (3 Replies)
Discussion started by: keenboy100
3 Replies

8. Shell Programming and Scripting

Rounding issue with awk

Hi Friends, I am trying to round following number. 0.07435000 echo "0.07435000"|awk '{printf "%s\n",$1*100}'|awk '{printf "%.2f\n",$1}' It returns: 7.435 It should return: 7.44 Any suggestion please? Thanks, Prashant (2 Replies)
Discussion started by: ppat7046
2 Replies

9. UNIX for Dummies Questions & Answers

Annoying rounding issue in awk

Hello I am getting this very annoying issue in awk: awk '{a=12825;b=a*1.25; print b}' test 16031.2 Thing is the multiplication result is wrong... Result should be 16031.25. I think the issue only happens on bigger numbers. What can I do to get passed this? Thanks by advance (3 Replies)
Discussion started by: Indalecio
3 Replies

10. Shell Programming and Scripting

awk calculation

Hallo all, I have a script which creates an output ... see below: root@a7germ:/tmp/pax > cat 20061117.txt 523.047 521.273 521.034 517.367 516.553 517.793 513.114 513.940 I would like to use awk to calculate the (a)total sum of the numbers (b) The average of the numbers. Please... (4 Replies)
Discussion started by: kekanap
4 Replies
Login or Register to Ask a Question

Featured Tech Videos