Skip to main content

FormatFloat

Struct FormatFloat 

Source
pub struct FormatFloat(pub f64);
Expand description

A Display adapter that writes a float exactly as CPython’s repr()/str() (identical for floats in Python 3): the shortest decimal string that round-trips, switching to scientific notation when the base-10 exponent is < -4 or >= 16, and always keeping at least one fractional digit (1.0, never 1) — 1e16"1e+16", 1234.5"1234.5", inf/nan lowercased.

This is the default rendering for a bare f"{x}", str(x), repr(x) and floats inside container reprs — not the format mini-language (that’s format_float_g et al, in monty). Rust can’t do this directly: its f64 Display never uses scientific notation (1e16 prints as 10000000000000000) and renders NaN as "NaN".

As a Display adapter it writes straight to the caller’s sink with no heap allocation: it borrows Rust’s shortest-digits guarantee via {:e} into a small stack buffer (an f64 {:e} is ASCII and ≤ 24 bytes) and re-lays-out those digits per CPython’s rules.

Tuple Fields§

§0: f64

Trait Implementations§

Source§

impl Display for FormatFloat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.