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

[Proposal] Add new overload to Math.Truncate #40456

Open
VBAndCs opened this issue Aug 6, 2020 · 6 comments
Open

[Proposal] Add new overload to Math.Truncate #40456

VBAndCs opened this issue Aug 6, 2020 · 6 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Milestone

Comments

@VBAndCs
Copy link

VBAndCs commented Aug 6, 2020

Background and Motivation

A common task is to truncate doubles to a specific places after the decimal point. There is no easy, fast and precise way to do this in Math class. Either we can round the number, or use this formula:

var n2 = Math.Truncate(n1 * Math.power(10, precision)) / 100;

more discussion about this is here:
https://stackoverflow.com/questions/3143657/truncate-two-decimal-places-without-rounding

Proposed API

So, I suggest to add a new overloads to the Truncate method:
Math.Truncate(double number, int precision)
Math.Truncate(decimal number, int precision)
so it can achieve this goal with a fast and accurate way to give us the best performance.

Usage Examples

var n2 = Math.Truncate(n1, precision); 
@VBAndCs VBAndCs added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Aug 6, 2020
@SingleAccretion
Copy link
Contributor

SingleAccretion commented Aug 6, 2020

I think you can now do this with Math.Round:

Math.Round(3.4611, 2, MidpointRounding.ToZero); //3.46
Math.Round(-3.4611, 2, MidpointRounding.ToZero); //-3.46
Math.Round(3.4679, 2, MidpointRounding.ToZero); //3.46
Math.Round(-3.4679, 2, MidpointRounding.ToZero); //-3.46

@ghost
Copy link

ghost commented Aug 6, 2020

Tagging subscribers to this area: @tannergooding, @pgovind
See info in area-owners.md if you want to be subscribed.

@VBAndCs
Copy link
Author

VBAndCs commented Aug 6, 2020

Thanks.
I am not aware of that option, and I think the reason is that the name MidpointRounding is misleading. These options are about the mid point (when the fraction digit is 5), and ir is not convenient to affect other non-midpoints.
So, either you change the name MidpointRounding, or add a third optional param to the round to specify the round (default), truncate or ceiling options.

@SingleAccretion
Copy link
Contributor

SingleAccretion commented Aug 6, 2020

@VBAndCs That is true and the only reason why I know about MidpointRounding.ToZero myself :).

@Gnbrkm41
Copy link
Contributor

Gnbrkm41 commented Aug 7, 2020

To me, Truncate means only one thing, and it's dropping the value past the decimal bit entirely instead of selectively rounding towards zero to a certain decimal points. This is true in libraries of other languages as well, where it does the same, for example, C/C++ <math.h>'s trunc function and Rust's f32::trunc. For that reason I don't think it's a great idea to expose these as an overload of Truncate.

I do believe that those behaviours you've described already exists in Round (by specifying MidpointRounding.ToZero). I do agree that the name "MidpointRounding" is very misleading. I think we should work towards fixing that instead - as @​SingleAccretion mentioned, ​#38160 tracks improving the existing Round to be more clearer.

Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity
Projects
None yet
Development

No branches or pull requests

5 participants