Skip to content

numfmt: --to-unit uses unscaled value to pick SI/IEC prefix #11666

@sylvestre

Description

@sylvestre

Summary

numfmt --to=<scale> --to-unit=N VALUE selects the output prefix (K, Ki, M, ...) based on the unscaled input VALUE, instead of the final scaled value VALUE / N. GNU selects the prefix based on the scaled result.

Found by fuzz_numfmt.

Reproduction

$ LC_ALL=C /usr/bin/numfmt --to=iec-i --to-unit=885 100000
113

$ target/debug/numfmt --to=iec-i --to-unit=885 100000
0.1Ki

100000 / 885 = 113, which is below 1 Ki (1024), so no suffix should be applied. uutils apparently looks at 100000, picks Ki, and emits 0.1Ki.

Another example:

$ LC_ALL=C /usr/bin/numfmt --to=iec-i --to-unit=885 1000000
1.2Ki
$ target/debug/numfmt --to=iec-i --to-unit=885 1000000
1.1Ki

Scaled value: 1000000 / 885 ≈ 1130, i.e. 1.2Ki (rounding up). uutils seems to compute the suffix from the unscaled value, giving 1.1Ki.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions