[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How are numbers printed in gawk?
From: |
Peng Yu |
Subject: |
Re: How are numbers printed in gawk? |
Date: |
Wed, 18 Mar 2020 09:17:26 -0500 |
On 3/18/20, Andrew J. Schorr <address@hidden> wrote:
> 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'.
Internal to awk, all numbers are considered as double? No numbers are integers?
--
Regards,
Peng