Skip to content

Fix Amount decimals handling#2951

Merged
apoelstra merged 3 commits intorust-bitcoin:masterfrom
Kixunil:reverse-amount-formatting-bug
Jul 4, 2024
Merged

Fix Amount decimals handling#2951
apoelstra merged 3 commits intorust-bitcoin:masterfrom
Kixunil:reverse-amount-formatting-bug

Conversation

@Kixunil
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil commented Jul 2, 2024

Displaying with minimal number of zeros is the default in Rust and we want to follow it. This was originally implemented in #716 but #2604 reversed it claiming this is common, however it broke people who rely on minimal display (e.g. BIP21) without fixing the root cause of #2136.

This reverts commit d887423 adds a test to prevent this change and also fixes the problem with {:0.8} not working.

Closes #2136
Closes #2948

Can we backport this one too?

@Kixunil Kixunil added the bug label Jul 2, 2024
@github-actions github-actions bot added the C-units PRs modifying the units crate label Jul 2, 2024
@coveralls

This comment was marked as outdated.

@Kixunil Kixunil force-pushed the reverse-amount-formatting-bug branch from ebf1ea8 to 0198881 Compare July 2, 2024 07:33
@coveralls

This comment was marked as outdated.

@coveralls

This comment was marked as outdated.

@Kixunil Kixunil force-pushed the reverse-amount-formatting-bug branch 2 times, most recently from eec469d to 80b7eb4 Compare July 2, 2024 09:16
@coveralls

This comment was marked as outdated.

@coveralls

This comment was marked as outdated.

@apoelstra
Copy link
Copy Markdown
Member

To be clear: the intended behavior is to display fewer zeros than the user requests with the {.X} formatter, even though this differs from the numeric types in stdlib?

@apoelstra
Copy link
Copy Markdown
Member

Ah, no, I see from your tests that you do want the new behavior of the precision specifier. But we have broken behavior of the width specifier, and I think what you are complaining about is the default behavior of no specifier is given.

Yeah, I can accept this, because your desired behavior is quite hard to obtain with the existing code. Even though I think {:.8} is what users will want overwhelmingly often, and now they need to write it explicitly.

And sure, I wouldn't mind backporting it since bip21 depends on it.

I am a little annoyed at your framing that you are "reverting" a PR that broke no tests because it "reverted intentional behavior", when both this PR and the "intentional behavior" one do many different things at once. And your complaint is not even about the main change in the "reverted" PR.

@yancyribbens
Copy link
Copy Markdown
Contributor

assert_eq!(format!("{}", Amount::from_sat(1000000)), "0.01 BTC"); looks good.

@Kixunil
Copy link
Copy Markdown
Collaborator Author

Kixunil commented Jul 2, 2024

But we have broken behavior of the width specifier

We haven't given that width is still the same. It just looks like we did because if both precision and width are set, precision must be exactly what specified (potentially overriding width) and width just adapts. This is consistent with Rust's floats. (And yes, I did a bunch of experiments to figure this out.)

do many different things at once

Yes, this is inaccurate. Since nobody ACKed yet, I'm going to rewrite the description.

And your complaint is not even about the main change in the "reverted" PR.

It is. The change in that PR forced {:08x} which was previously intentionally removed, so it made the whole situation strictly worse without fixing anything.

Kixunil added 2 commits July 2, 2024 18:38
Displaying with minimal number of zeros is the default in Rust and we
want to follow it. This was originally implemented in rust-bitcoin#716 but rust-bitcoin#2604
reversed it claiming this is common, however it broke people who rely on
minimal display (e.g. BIP21) without fixing the root cause of rust-bitcoin#2136.

This reverts commit d887423 adds a test
to prevent this change and also fixes the problem with `{:0.8}` not
working.

Closes rust-bitcoin#2136
Closes rust-bitcoin#2948
Low precision was previously not considered because it was believed it
could lead to misleading data being displayed. However it's quite
possible that people using low precision know what they are doing and
it's sometimes useful to show rounded numbers.

To enable low precision we just compute what the rounded number would be
and display that one instead.

This actually fully closes rust-bitcoin#2136 since this issue was mentioned there
along with previously-fixed displaying of higher precisions.
@Kixunil Kixunil force-pushed the reverse-amount-formatting-bug branch from 80b7eb4 to 467546f Compare July 2, 2024 16:40
@coveralls

This comment was marked as outdated.

tcharding
tcharding previously approved these changes Jul 2, 2024
Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 467546f

@Kixunil Kixunil changed the title Revert "Enforce displaying Amount with trailing zeros" Fix Amount decimals handling Jul 3, 2024
`fmt_value_in` was added when `display_in` wasn't available. However
common usage patterns seem to favor `display_in`. It can be used within
format strings and supports formatting options.

Removing it will simplify the codebase, so this deprecates it.
@coveralls
Copy link
Copy Markdown

coveralls commented Jul 3, 2024

Pull Request Test Coverage Report for Build 9772503688

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 28 of 31 (90.32%) changed or added relevant lines in 1 file are covered.
  • 45 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.06%) to 82.943%

Changes Missing Coverage Covered Lines Changed/Added Lines %
units/src/amount.rs 28 31 90.32%
Files with Coverage Reduction New Missed Lines %
units/src/amount.rs 15 86.86%
bitcoin/src/blockdata/script/borrowed.rs 30 79.21%
Totals Coverage Status
Change from base Build 9763162782: -0.06%
Covered Lines: 19504
Relevant Lines: 23515

💛 - Coveralls

@apoelstra
Copy link
Copy Markdown
Member

Thanks for changing the description. This looks like a correct description of the problem and history and I understand now why you were so upset by it.

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 3196c27

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 3196c27 I also really like how this reduces the complexity of the module, basically passing everything through to display_in

@apoelstra apoelstra merged commit 014c493 into rust-bitcoin:master Jul 4, 2024
@Kixunil Kixunil deleted the reverse-amount-formatting-bug branch July 4, 2024 13:27
tcharding added a commit to tcharding/rust-bitcoin that referenced this pull request Nov 14, 2024
We an use `Amount::display_in` now, this was done on master in PR rust-bitcoin#2951.
apoelstra added a commit that referenced this pull request Nov 15, 2024
107acf3 backport: Deprecate Amount::fmt_value_in (Tobin C. Harding)

Pull request description:

  We an use `Amount::display_in` now, this was done on master in PR #2951.

ACKs for top commit:
  shinghim:
    ACK 107acf3
  apoelstra:
    ACK 107acf3; successfully ran local tests

Tree-SHA512: b46156415a281caad5cd3238921f70dd8f575d700b901583fcdd9e037f51dab348d44efb1a74b385252a37d677d7fd7369ec7b623b680585a9427f1d1c448fb0
yancyribbens pushed a commit to yancyribbens/rust-bitcoin that referenced this pull request Dec 3, 2025
We an use `Amount::display_in` now, this was done on master in PR rust-bitcoin#2951.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug C-units PRs modifying the units crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amount to_string Precision Shouldn't Amount handle decimals formatting with {:.8} ?

5 participants