Skip to content

Improve document for std::fmt to format float type #112760

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions library/alloc/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@
//! Hello, ` 123` has 3 right-aligned characters
//! ```
//!
//! For the floating-point type, it employs the
//! [roundTiesToEven](https://en.m.wikipedia.org/wiki/Rounding#Rounding_half_to_even) convention:
Copy link
Member

Choose a reason for hiding this comment

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

What does the name "roundTiesToEven" refer to exactly? I did not find that anywhere in the link.

Comment on lines +276 to +277
Copy link
Member

Choose a reason for hiding this comment

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

This is unlikely to be the most appropriate link because the subsection you linked to is describing the operation of rounding a float to an integer, which is different in general from formatting a float to a particular precision. The text even calls this out: a "more sophisticated mode [is] used when rounding to significant figures".

//!
//! ```
//! println!("{:.0}", 12.5f64); // 12
//! println!("{:.0}", 12.3f64); // 12
//! println!("{:.0}", 11.6f64); // 12
//! println!("{:.0}", 11.5f64); // 12
//! println!("{:.0}", 11.4f64); // 11
//! println!("{:.0}", 10.5f64); // 10
//! println!("{:.0}", 9.5f64); // 10
//! println!("{:.0}", 8.5f64); // 8
//! println!("{:.0}", 7.5f64); // 8
//! println!("{:.0}", -7.5f64); // -8
//! println!("{:.0}", -8.5f64); // -8
//! println!("{:.0}", -9.5f64); // -10
//! println!("{:.0}", -10.5f64);// -10
//! println!("{:.0}", -11.4f64);// -11
//! println!("{:.0}", -11.5f64);// -12
//! println!("{:.0}", -11.6f64);// -12
//! println!("{:.0}", -12.5f64);// -12
//! println!("{:.0}", -12.6f64);// -13
//!
//! println!("{:.1}", 3.33f64); // 3.3
//! println!("{:.1}", -3.33f64);// -3.3
//! println!("{:.1}", -3.55f64);// -3.5
//! println!("{:.1}", 4.54f64); // 4.5
//! println!("{:.1}", 4.55f64); // 4.5
//! println!("{:.1}", 4.56f64); // 4.6
Comment on lines +280 to +304
Copy link
Member

@dtolnay dtolnay Jul 2, 2023

Choose a reason for hiding this comment

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

This seems like an extremely excessive number of examples. Can {:.1} with 1.25 and 1.75 be sufficient to illustrate what you mean?

//! ```
//!
Copy link
Member

Choose a reason for hiding this comment

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

There is already a paragraph above which describes how precision applies to floating point types. It would make more sense to me to explain the rounding directly in that paragraph.

//! For floating-point types, this indicates how many digits after the decimal point should be
//! printed.

//! ## Localization
//!
//! In some programming languages, the behavior of string formatting functions
Expand Down