Skip to content

numfmt: print what GNU prints for a whole number, and upper case the SI suffixes - #210

Merged
JalonSolov merged 1 commit into
vlang:mainfrom
metif12:fix/numfmt-numbers
Oct 4, 2026
Merged

JalonSolov merged 1 commit into
vlang:mainfrom
metif12:fix/numfmt-numbers

Conversation

@metif12

@metif12 metif12 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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.

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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants