bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How are numbers printed in gawk?


From: Andrew J. Schorr
Subject: Re: How are numbers printed in gawk?
Date: Wed, 18 Mar 2020 08:22:59 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Wed, Mar 18, 2020 at 01:26:01AM -0500, Peng Yu wrote:
> I'd like to write some python code that produces the same results as
> gawk for numbers. As far as I understand there is no integer type in
> gawk internal. But if I use float in python for integer, python will
> print something like 1.0. Therefore, I'd like to know gawk is
> implemented regarding numbers. Could anybody show the best approach to
> implement the same method used in awk dealing with numbers in python?
> Thanks.
> 
> $ awk 'BEGIN { print .0 + 1 }'
> 1
> $ python -c 'print(.0 + 1)'
> 1.0

The docs describe the two variables that control the conversion of numbers
to strings:

'CONVFMT'
     A string that controls the conversion of numbers to strings (*note
     Conversion::).  It works by being passed, in effect, as the first
     argument to the 'sprintf()' function (*note String Functions::).
     Its default value is '"%.6g"'.  'CONVFMT' was introduced by the
     POSIX standard.

And (more relevant for your case):

'OFMT'
     A string that controls conversion of numbers to strings (*note
     Conversion::) for printing with the 'print' statement.  It works by
     being passed as the first argument to the 'sprintf()' function
     (*note String Functions::).  Its default value is '"%.6g"'.
     Earlier versions of 'awk' used 'OFMT' to specify the format for
     converting numbers to strings in general expressions; this is now
     done by 'CONVFMT'.

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]