pub struct Float(_, _);Expand description
Human readable float.
Takes a floating point number as input and returns a ready-to-print!() Float.
The fractional floating point may or may not be rounded up/down in the String.
The default Float::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::from_2(3.0);
let f6 = Float::from_6(3.0);
let f9 = Float::from_9(f2.inner());
assert!(f2 == 3.00);
assert!(f6 == 3.000000);
assert!(f9 == 3.000000000);Cloning
Clone may be expensive:
// Probably cheap (stack allocated string).
let a = Float::from(100.0);
let b = a.clone();
// Probably expensive (heap allocated string).
let a = Float::from(f64::MAX);
let b = a.clone();The actual string used internally is not a String,
but a CompactString so that any string 24 bytes (12 bytes on 32-bit) or less are stack allocated instead of heap allocated.
The documentation will still refer to the inner string as a String. Anything returned will also be a String.
Float Errors
- Inputting
f64::NAN,f64::INFINITY,f64::NEG_INFINITYor thef32variants returns errors
To disable checks for these, (you are sure you don’t have NaN’s), enable the ignore_nan_inf feature flag.
Math
These operators are overloaded. They will always output a new Self:
Add +Sub -Div /Mul *Rem %
They can either be:
- Combined with another
Self:Float::from(1.0) + Float::from(1.0) - Or with the inner number itself:
Float::from(1.0) + 1.0
They also have the same panic!() behavior on overflow as the normal ones, because internally,
it is just calling .inner() $OPERATOR $NUMBER.
// Regular operators.
assert!(Float::from(10.0) + 10.0 == Float::from(20.0));
assert!(Float::from(10.0) - 10.0 == Float::from(0.0));
assert!(Float::from(10.0) / 10.0 == Float::from(1.0));
assert!(Float::from(10.0) * 10.0 == Float::from(100.0));
assert!(Float::from(10.0) % 10.0 == Float::from(0.0));Overflow example (floats don’t panic in this case):
let n = Float::from(f64::MAX) + f64::MAX;
assert!(n.is_inf());Examples
assert!(Float::from(0.0) == "0.000");
// This gets rounded up to '.568'
assert!(Float::from(1234.5678) == "1,234.568");
// To prevent that, use 4 point.
assert!(Float::from_4(1234.5678) == "1,234.5678");Implementations§
source§impl Float
impl Float
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the valid byte slice of the inner String
These bytes can always safely be used for std::str::from_utf8_unchecked.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
sourcepub fn head_dot(&self, len: usize) -> String
pub fn head_dot(&self, len: usize) -> String
Same as Self::head but returns a String ending with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_dot(4) == "2021...");sourcepub fn tail_dot(&self, len: usize) -> String
pub fn tail_dot(&self, len: usize) -> String
Same as Self::tail but returns a String starting with ...
This will return the full string without ... if
the len is longer than the actual inner str.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.tail_dot(5) == "...12-11");sourcepub fn head_tail(&self, head: usize, tail: usize) -> String
pub fn head_tail(&self, head: usize, tail: usize) -> String
Return the first head bytes and last tail
bytes of this string separated with ....
Since all readable types happen to only contain
ASCII characters, all char’s are equal to 1 byte.
let date = Date::from_str("2021-12-11").unwrap();
assert!(date.head_tail(3, 2) == "202...11");
assert!(date.head_tail(3, 3) == "202...-11");
assert!(date.head_tail(3, 5) == "202...12-11");sourcepub const fn zero() -> Self
pub const fn zero() -> Self
Returns a Float with the f64 value of 0.0.
The String is set to ZERO_FLOAT.
sourcepub const fn unknown() -> Self
pub const fn unknown() -> Self
Returns a Float with the f64 value of f64::NAN.
The String is set to UNKNOWN_FLOAT.
sourcepub const fn inf() -> Self
pub const fn inf() -> Self
Returns a Float with the f64 value of f64::INFINITY.
sourcepub fn is_nan(&self) -> bool
pub fn is_nan(&self) -> bool
Calls f64::is_nan.
sourcepub fn is_inf(&self) -> bool
pub fn is_inf(&self) -> bool
Calls f64::is_infinite.
sourcepub fn from_0(f: f64) -> Self
pub fn from_0(f: f64) -> Self
Same as Float::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
| Input | String Output |
|---|---|
| 0.0 | 0 |
| 50.123 | 50 |
| 100.1 | 100 |
sourcepub fn from_1(f: f64) -> Self
pub fn from_1(f: f64) -> Self
Same as Float::from but with 1 floating point.
sourcepub fn from_2(f: f64) -> Self
pub fn from_2(f: f64) -> Self
Same as Float::from but with 2 floating point.
sourcepub fn from_4(f: f64) -> Self
pub fn from_4(f: f64) -> Self
Same as Float::from but with 4 floating point.
sourcepub fn from_5(f: f64) -> Self
pub fn from_5(f: f64) -> Self
Same as Float::from but with 5 floating point.
sourcepub fn from_6(f: f64) -> Self
pub fn from_6(f: f64) -> Self
Same as Float::from but with 6 floating point.
sourcepub fn from_7(f: f64) -> Self
pub fn from_7(f: f64) -> Self
Same as Float::from but with 7 floating point.
sourcepub fn from_8(f: f64) -> Self
pub fn from_8(f: f64) -> Self
Same as Float::from but with 8 floating point.
sourcepub fn from_9(f: f64) -> Self
pub fn from_9(f: f64) -> Self
Same as Float::from but with 9 floating point.
sourcepub fn from_10(f: f64) -> Self
pub fn from_10(f: f64) -> Self
Same as Float::from but with 10 floating point.
sourcepub fn from_11(f: f64) -> Self
pub fn from_11(f: f64) -> Self
Same as Float::from but with 11 floating point.
sourcepub fn from_12(f: f64) -> Self
pub fn from_12(f: f64) -> Self
Same as Float::from but with 12 floating point.
sourcepub fn from_13(f: f64) -> Self
pub fn from_13(f: f64) -> Self
Same as Float::from but with 13 floating point.
sourcepub fn from_14(f: f64) -> Self
pub fn from_14(f: f64) -> Self
Same as Float::from but with 14 floating point.
sourcepub fn from_15(f: f64) -> Self
pub fn from_15(f: f64) -> Self
Same as Float::from but with 15 floating point.
sourcepub fn from_16(f: f64) -> Self
pub fn from_16(f: f64) -> Self
Same as Float::from but with 16 floating point.
sourcepub fn from_17(f: f64) -> Self
pub fn from_17(f: f64) -> Self
Same as Float::from but with 17 floating point.
sourcepub fn from_18(f: f64) -> Self
pub fn from_18(f: f64) -> Self
Same as Float::from but with 18 floating point.
Trait Implementations§
source§impl<'__de> BorrowDecode<'__de> for Float
impl<'__de> BorrowDecode<'__de> for Float
source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>
source§impl<'de> Deserialize<'de> for Float
impl<'de> Deserialize<'de> for Float
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl PartialEq<&Float> for f64
impl PartialEq<&Float> for f64
source§impl PartialEq<&Float> for str
impl PartialEq<&Float> for str
source§impl PartialEq<&str> for Float
impl PartialEq<&str> for Float
source§impl PartialEq<Float> for Float
impl PartialEq<Float> for Float
source§impl PartialEq<Float> for f64
impl PartialEq<Float> for f64
source§impl PartialEq<Float> for str
impl PartialEq<Float> for str
source§impl PartialOrd<&Float> for f64
impl PartialOrd<&Float> for f64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&Float> for str
impl PartialOrd<&Float> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<&str> for Float
impl PartialOrd<&str> for Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Float> for Float
impl PartialOrd<Float> for Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Float> for f64
impl PartialOrd<Float> for f64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<Float> for str
impl PartialOrd<Float> for str
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<f64> for &Float
impl PartialOrd<f64> for &Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<f64> for Float
impl PartialOrd<f64> for Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl PartialOrd<str> for Float
impl PartialOrd<str> for Float
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere T: Display,
source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more