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

Preserve '.0' when Displaying Number #919

Merged
merged 3 commits into from
Aug 21, 2022
Merged

Preserve '.0' when Displaying Number #919

merged 3 commits into from
Aug 21, 2022

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Aug 21, 2022

Tiny followup to #918.

let number: serde_json::Number = serde_json::from_str("1.0").unwrap();
println!("{}", number);

Previously: 1
Now: 1.0

src/number.rs Outdated
Comment on lines 295 to 298
N::PosInt(u) => Display::fmt(&u, formatter),
N::NegInt(i) => Display::fmt(&i, formatter),
N::Float(f) => Display::fmt(&f, formatter),
// Preserve `.0` on integral values, which Display hides
N::Float(f) => Debug::fmt(&f, formatter),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should just serialize using ryu and itoa, like the Serializer does, or maybe even go through the Serializer itself. std::fmt produces a worse representation for floats, like 120000000000000000000000000000000000000000 instead of 1.2e41.

@dtolnay dtolnay merged commit 2c8e2b0 into master Aug 21, 2022
@dtolnay dtolnay deleted the displaynum branch August 21, 2022 21:07
@iitalics
Copy link

shouldn't this be a more significant version bump than patch? it has broken a unit test of mine. nothing extreme but was not expecting to see this behavior change

@Directory
Copy link

rustaceans are afraid of MAJOR semvar version changes. it's like kryptonite to them.

@alteous
Copy link

alteous commented Oct 12, 2023

Could this be configurable? I have a slightly unusual use case where I need to represent ones' complement integer, i.e., signed integers with a negative zero. I'm using f64::serialize but that means integers are displayed as 1.0 instead of 1 which looks weird in this context.

@iitalics
Copy link

@alteous it looks like you could configure some things through the Formatter trait

@alteous
Copy link

alteous commented Oct 12, 2023

@iitalics that looks promising, thanks! I'll give it a try.

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

Successfully merging this pull request may close these issues.

4 participants