From 730560b982006c35f811a5de53fe8e1fa422c024 Mon Sep 17 00:00:00 2001 From: Tristan F Date: Mon, 12 Feb 2024 08:20:13 -0500 Subject: [PATCH 1/3] docs: mention round-to-even in precision formatting --- library/alloc/src/fmt.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index fce2585cbf5c4..acff026b91bb2 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -278,6 +278,19 @@ //! Hello, ` 123` has 3 right-aligned characters //! ``` //! +//! When truncuating these values, Rust uses round-to-even, which may +//! cause concern when formatting for scientific notation. For example, +//! +//! ``` +//! print!("{0:.1$e}", 12345, 3); +//! ``` +//! +//! Would return: +//! +//! ```text +//! 1.234e4 +//! ``` +//! //! ## Localization //! //! In some programming languages, the behavior of string formatting functions From 30f66659535c1eb37f98b42b42323089c1a21bb0 Mon Sep 17 00:00:00 2001 From: "Tristan F." Date: Mon, 12 Feb 2024 14:43:19 +0000 Subject: [PATCH 2/3] style: fmt --- library/alloc/src/fmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index acff026b91bb2..6899ff4711feb 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -284,7 +284,7 @@ //! ``` //! print!("{0:.1$e}", 12345, 3); //! ``` -//! +//! //! Would return: //! //! ```text From 0f53e720a852cad485daab8d5882ef498d8905bd Mon Sep 17 00:00:00 2001 From: Tristan F <26509014+LeoDog896@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:17:47 -0500 Subject: [PATCH 3/3] docs: use correct link, use secondary example --- library/alloc/src/fmt.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index 6899ff4711feb..b9918752540f3 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -278,17 +278,20 @@ //! Hello, ` 123` has 3 right-aligned characters //! ``` //! -//! When truncuating these values, Rust uses round-to-even, which may -//! cause concern when formatting for scientific notation. For example, +//! When truncating these values, Rust uses [round half-to-even](https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even), +//! which is the default rounding mode in IEEE 754. +//! For example, //! //! ``` //! print!("{0:.1$e}", 12345, 3); +//! print!("{0:.1$e}", 12355, 3); //! ``` //! //! Would return: //! //! ```text //! 1.234e4 +//! 1.236e4 //! ``` //! //! ## Localization