Struct readable::Float

source ·
pub struct Float(_, _);
Expand description

Human readable float.

Takes a floating point number as input and returns a ready-to-print!() Float.

The default From implementation will print 3 decimal numbers.

This can be changed by using different functions when initially creating the Float, or converting an existing Float, for example:

let f2 = Float::new_2_point(3.0);
let f6 = Float::new_6_point(3.0);
let f9 = Float::new_9_point(f2.to_f64());

println!("{}\n{}\n{}", f2, f6, f9);

// 3.00
// 3.000000
// 3.000000000

Exceptions

| Exceptions | String Output | |—————————————––|—————––| | f64::NAN | NaN | f64::INFINITY & f64::NEG_INFINITY |

To disable checks for these, (you are sure you don’t have NaN’s), enable the ignore_nan_inf feature flag.

Examples

| Input | Output | |––––––––––|—————––| | 0.0 | 0.000 | 1234.571 | 1,234.571 | 1234.571 | 1,234.571000 | 99.123 (percent) | 99.12% | 0.001 (percent) | 0%

Implementations§

source§

impl Float

source

pub fn zero() -> Self

Returns a Self with the f64 value of 0.0.

The String is set to 0.000.

source

pub fn unknown() -> Self

Returns a Self with the f64 value of f64::NAN.

The String is set to ???.

source

pub fn nan() -> Self

Returns a Self with the f64 value of f64::NAN.

The String is set to NaN.

source

pub fn inf() -> Self

Returns a Self with the f64 value of f64::INFINITY.

The String is set to .

source

pub fn percent(f: f64) -> Self

Truncates to 2 floating point and appends a %.

Anything lower than 0.01 is rounded down to 0.00.

Examples
InputString Output
0.00.00%
0.0010.00%
0.010.01%
0.10.10%
1.01.00%
50.050.00%
100.0100.00%
150.0150.00%
1000.01,000.00%
250000.0250,000.00%
source

pub fn as_str(&self) -> &str

Return a borrowed str without consuming Self.

source

pub fn to_f64(&self) -> f64

Returns the inner f64.

source

pub fn into_string(self) -> String

Consumes [Self], returning the inner String.

source

pub fn into_raw(self) -> (f64, String)

Consumes Self, returning the inner f64 and String.

source

pub fn new_0_point(f: f64) -> Self

Same as Self::from but with no floating point on the inner String.

The inner f64 stays the same as the input.

This does not round up or down, it completely ignores the floating point.

Examples
InputString Output
0.00
50.12350
100.1100
source

pub fn new_1_point(f: f64) -> Self

Create a new Self but with 1 floating point.

source

pub fn new_2_point(f: f64) -> Self

Create a new Self but with 2 floating point.

source

pub fn new_4_point(f: f64) -> Self

Create a new Self but with 4 floating point.

source

pub fn new_5_point(f: f64) -> Self

Create a new Self but with 5 floating point.

source

pub fn new_6_point(f: f64) -> Self

Create a new Self but with 6 floating point.

source

pub fn new_7_point(f: f64) -> Self

Create a new Self but with 7 floating point.

source

pub fn new_8_point(f: f64) -> Self

Create a new Self but with 8 floating point.

source

pub fn new_9_point(f: f64) -> Self

Create a new Self but with 9 floating point.

source

pub fn new_10_point(f: f64) -> Self

Create a new Self but with 10 floating point.

source

pub fn new_11_point(f: f64) -> Self

Create a new Self but with 11 floating point.

source

pub fn new_12_point(f: f64) -> Self

Create a new Self but with 12 floating point.

source

pub fn new_13_point(f: f64) -> Self

Create a new Self but with 13 floating point.

source

pub fn new_14_point(f: f64) -> Self

Create a new Self but with 14 floating point.

source

pub fn percent_0_point(f: f64) -> Self

Same as Self::percent but with no floating point on the inner String.

The inner f64 stays the same as the input.

This does not round up or down, it completely ignores the floating point.

Examples
InputString Output
0.00%
50.12350%
100.1100%
source

pub fn percent_1_point(f: f64) -> Self

Same as Self::percent but with 1 floating point.

source

pub fn percent_2_point(f: f64) -> Self

Same as Self::percent but with 2 floating point.

source

pub fn percent_4_point(f: f64) -> Self

Same as Self::percent but with 4 floating point.

Trait Implementations§

source§

impl Clone for Float

source§

fn clone(&self) -> Float

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Float

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Float

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Float

source§

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

Formats the value using the given formatter. Read more
source§

impl From<f32> for Float

source§

fn from(number: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for Float

source§

fn from(number: f64) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Float

source§

fn from(number: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Float

source§

fn from(number: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Float

source§

fn from(number: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Float

source§

fn from(number: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Float

source§

fn from(number: usize) -> Self

Converts to this type from the input type.
source§

impl PartialEq<&str> for Float

source§

fn eq(&self, other: &&str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Float> for Float

source§

fn eq(&self, other: &Float) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Float> for f64

source§

fn eq(&self, other: &Float) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Float> for str

source§

fn eq(&self, other: &Float) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<f64> for Float

source§

fn eq(&self, other: &f64) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for Float

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Float> for Float

source§

fn partial_cmp(&self, other: &Float) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Float

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl StructuralPartialEq for Float

Auto Trait Implementations§

§

impl RefUnwindSafe for Float

§

impl Send for Float

§

impl Sync for Float

§

impl Unpin for Float

§

impl UnwindSafe for Float

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToCompactString for Twhere T: Display,

source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

source§

default fn to_string(&self) -> String

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

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,