[Solved] awk Errors on notation

can someone spot what i'm doing wrong here:

awk 'BEGIN{printf("%0.2f", 1 / 2649320) * 100}'

i get this error:

awk: line 1: syntax error at or near *

then i do this and get the answer i'm trying to avoid:

awk 'BEGIN{print(1 / 2649320) * 100}'
3.77455e-05

The awk utility is interpreting:

awk 'printf("%0.2f", 1 / 2649320) * 100'

as:

(printf("%0.2f", 1 / 2649320)) * 100

and you are getting an error because the awk printf() function does not return a numeric value. If I understand what you're trying to do, try:

awk 'BEGIN{printf("%0.2f", (1 / 2649320) * 100)}'

or:

awk 'BEGIN{printf("%0.2f", 100 / 2649320)}'
1 Like

Well, I am not sure why printf behaves that way, may it is not programmed to return the value but just print it out? May be it is not a function?

However, I think may be you want this?

awk 'BEGIN{print sprintf("%0.2f", 1 / 2649320) * 100}'

--ahamed

1 Like

sorry guys. i should have gave more explanation. the answer:

3.77455e-05

is correct.

i just want to get rid of the notation. and i was hoping the 0.2f would deal with that but evidently, it's not. :frowning:

You need to increase the decimal range

# awk 'BEGIN{printf("%0.10f", 100 / 2649320)}'
0.0000377455

--ahamed

1 Like

I hope that ahamed101 has already suggested the solution you're looking for, but I'm not sure we understand what you mean by "i (sic) just want to get rid of the notation".

If you aren't looking for fixed decimal notation with 10 digits after the radix character, what notation do you want? You might also want to look at the awk man page's description of the OFMT variable (which has a default value of %.6g ).

1 Like

Hi.

I'm guessing that he means the common phrase scientiic notation with e, as in e-05 ... cheers, drl

... usually referred to as (scientific) E notation or (scientific) e notation, rather than (scientific) exponential notation (though the latter also occurs). The use of this notation is not encouraged in publications ... from Scientific notation - Wikipedia, the free encyclopedia

1 Like

thanks so much everyone. problem has been resolved :slight_smile: