Skip to content

[kubernetes] Format negative quantities with SI/binary suffixes - #4956

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/core-negative-quantity-suffix
Oct 23, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/core-negative-quantity-suffix

Conversation

@yue9944882

Copy link
Copy Markdown
Member

Bug

QuantityFormatter.removeFactorsForBase used result.compareTo(base) >= 0 as its loop guard. For a negative unscaled value that comparison is always false, so the factor-removal loop never ran for negative quantities and they were never reduced to a suffixed form.

As a result Quantity.toSuffixedString() produced unsuffixed output for negative values:

  • -100000 (DECIMAL_SI) formatted as "-100000" instead of "-100k"
  • -1048576 (BINARY_SI) formatted as "-1048576" instead of "-1Mi"

Minimal fix

Guard on the absolute value: while (result.abs().compareTo(base) >= 0 && ...). BigInteger.mod already returns a non-negative remainder and BigInteger.divide handles negative values, so the divisibility check and the downstream exponent / multiple-of-3 math are unchanged for positive inputs. Behavior-preserving except for the bug; one-line change.

Verification

Added two focused unit tests in QuantityFormatterTest (formatNegativeDecimalSi, formatNegativeBinarySi). They fail before the fix (expected "-100k" but was "-100000"; expected "-1Mi" but was "-1048576") and pass after.

./mvnw -pl kubernetes -am test -Dtest=QuantityFormatterTest,QuantityTest,SuffixFormatterTest

  • QuantityFormatterTest: 22 run, 0 failures
  • QuantityTest: 1 run, 0 failures
  • SuffixFormatterTest: 16 run, 0 failures

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 22, 2026
@kubernetes-prow kubernetes-prow Bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 22, 2026
QuantityFormatter.removeFactorsForBase used result.compareTo(base) >= 0
as the loop guard, which is always false for negative unscaled values, so
negative quantities were never reduced to a suffixed form. As a result
Quantity.toSuffixedString() returned e.g. "-100000" instead of "-100k"
and "-1048576" instead of "-1Mi".

Guard on the absolute value so the factor-removal loop runs for negative
magnitudes too. BigInteger.mod already returns a non-negative remainder
and divide handles negative values, so the divisibility check and
downstream exponent math are unchanged for positive inputs.

Add focused unit tests covering negative DECIMAL_SI and BINARY_SI
formatting (fail before, pass after).
@yue9944882
yue9944882 force-pushed the fix/core-negative-quantity-suffix branch from 7bc00df to ac0b1c6 Compare October 22, 2026 17:27

@brendandburns brendandburns left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 23, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, yue9944882

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [brendandburns,yue9944882]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@brendandburns
brendandburns marked this pull request as ready for review October 23, 2026 14:00
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 23, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 98927e0 into kubernetes-client:master Oct 23, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants