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

truncateAsFixed() idea #56039

Open
shmink opened this issue Jun 18, 2024 · 1 comment
Open

truncateAsFixed() idea #56039

shmink opened this issue Jun 18, 2024 · 1 comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation Issues triaged by automation (see https://github.com/dart-lang/sdk/wiki/Triage-automation) type-enhancement A request for a change that isn't a bug

Comments

@shmink
Copy link

shmink commented Jun 18, 2024

Hello, I ran into an issue today at work (using flutter at work) where I was receiving a double from the backend that was 116.41666666. I wanted to display this value as an amount of money, such as £116.41 and I didn't expect any rounding to occur.

Unfortunately, I found out that toStringAsFixed() does appear to round as I was getting £116.42 on the display. I searched but I didn't see any kind of way to effectively truncate a double to a fixed amount.

In the end, I created a function to do it for me. I don't think it's great but maybe a jumping-off point for a feature that I think would be useful to have in the SDK.

https://dartpad.dev/?id=e8ab76b086cbd79b212491b38bf58a7f


Only now of writing this do I see on the docs the example line toStringAsFixed()

(4321.12345678).toStringAsFixed(5);  // 4321.12346

where it shows rounding but my personal opinion is that it's not obvious enough. Perhaps instead

(1.118).toStringAsFixed(2);  // 1.12

maybe it's the 1, 2, 3, ... it doesn't stand out.

Either way, kind regards, Tom.

@dart-github-bot
Copy link
Collaborator

Labels: area-core-library, type-enhancement

Summary: The user proposes a new function truncateAsFixed() to truncate a double to a fixed number of decimal places without rounding, as toStringAsFixed() currently rounds the value. The user believes the rounding behavior of toStringAsFixed() is not sufficiently clear in the documentation.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation Issues triaged by automation (see https://github.com/dart-lang/sdk/wiki/Triage-automation) type-enhancement A request for a change that isn't a bug labels Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation Issues triaged by automation (see https://github.com/dart-lang/sdk/wiki/Triage-automation) type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants