Repository navigation
numfmt: print what GNU prints for a whole number, and upper case the SI suffixes - #210
Merged
Merged
Conversation
…SI suffixes Four defects, all measured against GNU 9.4 and uutils 0.0.17 rather than reasoned about. Twenty cases, five numbers in each of --to=si, --to=iec and --to=iec-i plus five bare numbers: 8 of 20 agreed before, 20 of 20 now. numfmt 1000 1000.0 -> 1000 numfmt 0.5 1 -> 0.5 numfmt -12.25 -13 -> -12.25 numfmt --to=si 1500 1.5k -> 1.5K numfmt 2000000.6 2.0000006e+06 -> 2000000.6 - f64.str() is not how GNU prints a number. It keeps a ".0" on a whole one and switches to scientific notation on a large fractional one, so both the whole and the fractional case were wrong in opposite directions. - A number with no suffix and no unit to convert is handed back as it is, which is what --to=none means. It was being rounded to an integer first, away from zero, so 0.5 answered 1 and -12.25 answered -13. Rounding now happens only when there is something to convert by, and it goes through the function that honours --round, which scale_number was not doing: it always rounded from zero. - SI suffixes are upper case at GNU. The one that said so in this file was power_to_suffix, which nothing called; the list that was in use said lower case. power_to_suffix is gone rather than left as the second answer to the same question. ## Where this still differs, on purpose GNU rejects a suffix in the input unless --from is given: numfmt 2K numfmt: rejecting suffix in input: '2K' (consider using --from) This port has no --from at all, only --from-unit, so the four test cases that pass a suffix are left asserting what it does instead - 2K is 2000. That is recorded in the test as this port's behaviour and not GNU's, rather than quietly agreeing with it. Adding --from is the fix, and it is a separate piece of work. ## Tests src/numfmt is 1 of 1, the twenty-case comparison against GNU 9.4 is 20 of 20, and `v fmt -verify src/numfmt` passes. Five of the expectations in test_to_options changed, and every one of them is a string read off GNU rather than a value this implementation produced.
This was referenced Oct 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
numfmt: print what GNU prints for a whole number, and upper case the SI suffixes
Four defects, all measured against GNU 9.4 and uutils 0.0.17 rather than reasoned
about. Twenty cases, five numbers in each of --to=si, --to=iec and --to=iec-i plus
five bare numbers: 8 of 20 agreed before, 20 of 20 now.
f64.str() is not how GNU prints a number. It keeps a ".0" on a whole one and
switches to scientific notation on a large fractional one, so both the whole and
the fractional case were wrong in opposite directions.
A number with no suffix and no unit to convert is handed back as it is, which is
what --to=none means. It was being rounded to an integer first, away from zero, so
0.5 answered 1 and -12.25 answered -13. Rounding now happens only when there is
something to convert by, and it goes through the function that honours --round,
which scale_number was not doing: it always rounded from zero.
SI suffixes are upper case at GNU. The one that said so in this file was
power_to_suffix, which nothing called; the list that was in use said lower case.
power_to_suffix is gone rather than left as the second answer to the same
question.
Where this still differs, on purpose
GNU rejects a suffix in the input unless --from is given:
This port has no --from at all, only --from-unit, so the four test cases that pass
a suffix are left asserting what it does instead - 2K is 2000. That is recorded in
the test as this port's behaviour and not GNU's, rather than quietly agreeing with
it. Adding --from is the fix, and it is a separate piece of work.
Tests
src/numfmt is 1 of 1, the twenty-case comparison against GNU 9.4 is 20 of 20, and
v fmt -verify src/numfmtpasses. Five of the expectations in test_to_optionschanged, and every one of them is a string read off GNU rather than a value this
implementation produced.