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 · 4 comments · May be fixed by #5834
Open

Add leading/trailing zeros to floats #3187

alok opened this issue Nov 8, 2018 · 4 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 😁

@Keavon
Copy link

Keavon commented Jan 1, 2025

The proposed Never mode would perhaps need a choice for what to do in range syntax. Some possibilities include:

  • 0.0..1.0 (when used in range, uses Always)
  • 0.0..1. (when used in range, uses Always for the LHS and Never for the RHS)
  • (0.)..1. (when used in range, wraps the LHS in parentheses)
  • Any more cases I'm forgetting?

That said, I really appreciate having Never since I currently have to manually enforce the removal of trailing zeros during code review at the moment in my open source code base. I point these out, but I hope the optionality here doesn't slow down or de-scope the overall effort by having perfect be the enemy of good. Picking any of those would be appreciated over skipping or delaying the Never option entirely.

@amatveiakin
Copy link

@Keavon Thanks for pointing this out!
I went with another option in #5834: 0. ..1.
While it looks a bit awkward, I think it's the least awkward option of all. IMO 0.0..1. feels unbalanced and 0.0..1.0 feels excessive; also there is the questions of what to do with things like 1.0/2.0..1.0. With (0.)..1. I don't like the fact that switching from Always to Never and then back to Always would produce irreversible changes (or we would need to remove the parens, which is something rustfmt currently doesn't do).
Also there was already a precedent of adding a space between a float and .. in rustfmt, so it's consistent with the current formatting practice.

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.

5 participants