Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply Math.Round to FeeRate to mitigate rounding issues #1189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NBitcoin/FeeRate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public FeeRate(Money feePaid, int size)
if (feePaid.Satoshi < 0)
throw new ArgumentOutOfRangeException(nameof(feePaid), "Cannot be less than 0.");
if (size > 0)
_FeePerK = (long)((decimal)feePaid.Satoshi / (decimal)size * 1000m);
_FeePerK = (long)Math.Round((decimal)feePaid.Satoshi / size * 1000m, MidpointRounding.AwayFromZero);
Copy link
Contributor

Choose a reason for hiding this comment

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

@lukasdll just curious, if the LOC said _FeePerK = (long)((decimal)feePaid.Satoshi / (decimal)(size * 1000)); instead of _FeePerK = (long)((decimal)feePaid.Satoshi / (decimal)size * 1000m);, wouldn't the automatic rounding done by the cast been better? I mean, your fix is probably better than what I just said, but just thinking that the cast being done after the multiplication would have been better before

Copy link
Contributor

Choose a reason for hiding this comment

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

nevermind, I was looking at the wrong cast (I guess it's the (long) cast that is problematic, not the (decimal) one)

else
_FeePerK = Money.Zero;
}
Expand Down