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

Add leading/trailing zeros to floats #3187

Open
alok opened this issue Nov 8, 2018 · 2 comments · May be fixed by #5834
Open

Add leading/trailing zeros to floats #3187

alok opened this issue Nov 8, 2018 · 2 comments · May be fixed by #5834

Comments

@alok
Copy link

alok commented Nov 8, 2018

Essentially, these reformats should be done for consistency and readability:

x. -> x.0
.x -> 0.x

So 1. would become 1.0 and .1 would become 0.1.

@amatveiakin
Copy link

amatveiakin commented Jan 26, 2024

Sent a pull request to implement this.

Note that .x is not a concern, since it's not a valid floating-point literal in Rust (in contrast to C++).

Also I believe it would be nice to support different formatting styles. I came up with the following options:

  • Preserve (default). Leave the literal as-is.
  • Always. Add a trailing zero to the literal. E.g. 1.0, 2.0e10, 3.0f32.
  • IfNoPostfix. Add a trailing zero by default. If the literal contains an exponent or a suffix, the zero and the preceding period are removed. E.g. 1.0, 2e10, 3f32.
  • Never. Remove the trailing zero. If the literal contains an exponent or a suffix, the preceding period is also removed. E.g. 1., 2e10, 3f32.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 27, 2024

Thanks for taking the time to look into this and for submitting a PR! I think this is a configuration rustfmt could support, and the different options you've outlined here make sense to me. I'll try to carve out some time over the next few days to review and provide feedback on your implementation 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants