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
Round for doubles should take the number of decimals as argument... #8575
Comments
Removed Type-Defect label. |
This comment was originally written by adrian.avila.mtz...@gmail.com No plans for fixing this one yet? Has been a while. |
Looks like Dart already has this but it's named strangely, the docs are vague about the rounding part and it returns a string.
Results in The docs should mention that this method actually does rounding and not simply truncate. Also, it would make more sense to add this capability to the existing |
|
@zoechi |
@eukreign I don't argue against that. I think that it just can't be integrated into |
What if I need a double with precision, not a string. i.e . need 1.00 or 2.00 . |
Yes |
That's not a Flutter peculiarity. It's an IEEE-754 floating point peculiarity which is shared by all languages using normal floating points. The value of Dart chooses to give different double values different That's not to say that we can't have a rounding operation which rounds to a specific number of decimal digits (and then to the nearest representable double value). |
That's exactly what the original question is asking for...not to mention my comment as well. |
Yes. That was an attempt to get back on track, and not discuss |
@lrhn It was never off track. You're the only who's mentioned |
I have a solution. It's kinda of a hack.
|
@YoussefLasheen The original post stated that he wanted to specify the amount of decimal precision with a simple integer (1, 2, 3 etc). I already made this kind of formula like yours as a workaround too but I think also the point also is we want it built into Dart like |
Taking @YoussefLasheen's idea a bit further, we can write our own extension methods. Keep in mind that working with doubles is not precise and can give unexpected results. Also, a proper implementation for this should also
|
Working with doubles while requiring precision is never easy. They are designed to be lossful. Example: var x = (pow(2,43) - 1) + .0625;
print(x); // 8796093022207.0625
print(x.roundDigits(5)); // 8796093022207.063 |
@lrhn I agree with everything you say. The thread itself asks for something that doubles are not able to do properly. Even so, there might be cases where the proposed solution is useful. |
The functionality can definitely exist. We have the code which does We want to avoid doing something which gives a wrong result in some situations. Either you can trust the function, or you can't. It's not enough that it works for small numbers. (Obviously, if we know that it works for small numbers below some threshold, we can use it for small numbers and fall back on |
Tested @andreidiaconu ' s methods for a workaround and it works for me. |
IMO, people who care about decimal representations probably should consider using something like |
This issue was originally filed by adrian.avil...@gmail.com
If I have the number 12.346 and I need to round it to only two decimals is not possible, all I have is 12.346.round(), and the result is 12.0. It would be ideal to do 12.346.round(2) so the result can be 12.35.
References:
In C# there is Math.Round(double, decimals)
In Pascal there is RoundTo(double, decimals).
The text was updated successfully, but these errors were encountered: