bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: gawk uses default OFMT with sprintf("%s",...) and when concatenating


From: Aharon Robbins
Subject: Re: gawk uses default OFMT with sprintf("%s",...) and when concatenating strings
Date: Mon, 30 Aug 2010 22:30:32 +0300
User-agent: Heirloom mailx 12.4 7/29/08

Greetings. Re this:

> Date: Mon, 30 Aug 2010 18:57:13 +0000 (GMT)
> From: Christer Lundberg <address@hidden>
> Subject: gawk uses default OFMT with sprintf("%s",...) and when concatenating 
> strings
> To: address@hidden
>
> Version: gawk 3.1.7 (Fedora package gawk-3.1.7-3.fc12.i686)
>
> Sample program:
> ? BEGIN{ OFMT="%.8g"
> ???????? x = 1/7
> ???????? print "print with a comma????? :", x
> ???????? print "concatenated to a string: " x
> ???????? print "using sprintf and %s??? :", sprintf("%s",x)
> ?????? }
>
> Output with my system:
> print with a comma????? : 0.14285714
> concatenated to a string: 0.142857
> using sprintf and %s??? : 0.142857
>
> Expected output:
> Would expect eight decimals on each one of the three rows.

Thanks for taking the time to send in a report.

However, it's not a bug.  Gawk uses OFMT *only* when converting
numeric values in a print statements.  The variable CONVFMT (with
the same initial value as OFMT) controls conversion of numbers to strings
in expressions, which is what's happening in the latter two statements.
If you change the assignment to

        CONVFMT = OFMT = "%.8g"

you will get the output you're expecting.

Thanks,

Arnold



reply via email to

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