Skip to content

awk: numbers are printed with full precision, OFMT and CONVFMT are ignored #175

Open

Description

@sylvestre

Non-integer numbers are converted to strings with Rust's shortest round-trip
representation. POSIX awk uses OFMT (default %.6g) for print and
CONVFMT (default %.6g) for every other number-to-string conversion
(concatenation, array subscripts, ...). So print 1/3 gives
0.3333333333333333 instead of 0.333333, and array keys built from
non-integer numbers differ.

Reproduction

Rust (incorrect)

$ ./target/release/awk 'BEGIN { x = 1 / 3; print x; y = x ""; print y; OFMT = "%.2f"; print 3.14159 }'
0.3333333333333333
0.3333333333333333
3.14159

GNU (correct)

$ gawk 'BEGIN { x = 1 / 3; print x; y = x ""; print y; OFMT = "%.2f"; print 3.14159 }'
0.333333
0.333333
3.14

Test

#[test]
fn print_uses_ofmt_and_concat_uses_convfmt() {
    ucmd()
        .arg("BEGIN { v = 2 / 7; print v; print v \"\"; OFMT = \"%.3f\"; print v; print v \"\" }")
        .succeeds()
        .stdout_only("0.285714\n0.285714\n0.286\n0.285714\n");
}

Exposed by the GNU awk test suite: divzero2, getnr2tb, and many tests that
are now stopped earlier by printf.

Activity

  1. added
    bug
    gnu-compat
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions