pub enum NumberPrefix<F> {
Standalone(F),
Prefixed(Prefix, F),
}
Expand description
The result of trying to apply a prefix to a floating-point value.
Variants§
Standalone(F)
A standalone value is returned when the number is too small to have any prefixes applied to it. This is commonly a special case, so is handled separately.
Prefixed(Prefix, F)
A prefixed value is large enough for prefixes. This holds the prefix, as well as the resulting value.
Implementations§
Source§impl<F: Amounts> NumberPrefix<F>
impl<F: Amounts> NumberPrefix<F>
Sourcepub fn decimal(amount: F) -> Self
pub fn decimal(amount: F) -> Self
Formats the given floating-point number using decimal prefixes.
This function accepts both f32
and f64
values. If you’re trying to
format an integer, you’ll have to cast it first.
§Examples
use unit_prefix::{NumberPrefix, Prefix};
assert_eq!(
NumberPrefix::decimal(1_000_000_000_f32),
NumberPrefix::Prefixed(Prefix::Giga, 1_f32)
);
Examples found in repository?
examples/conversions.rs (line 35)
29fn main() {
30 // part one, decimal prefixes
31 let mut n = 1_f64;
32 for _ in 0..8 {
33 n *= 1000_f64;
34
35 let decimal = format_prefix(NumberPrefix::decimal(n));
36 let binary = format_prefix(NumberPrefix::binary(n));
37 println!("{:26} bytes is {} and {:10}", n, decimal, binary);
38 }
39
40 println!();
41
42 // part two, binary prefixes
43 let mut n = 1_f64;
44 for _ in 0..8 {
45 n *= 1024_f64;
46
47 let decimal = format_prefix(NumberPrefix::decimal(n));
48 let binary = format_prefix(NumberPrefix::binary(n));
49 println!("{:26} bytes is {} and {:10}", n, binary, decimal);
50 }
51}
Sourcepub fn binary(amount: F) -> Self
pub fn binary(amount: F) -> Self
Formats the given floating-point number using binary prefixes.
This function accepts both f32
and f64
values. If you’re trying to
format an integer, you’ll have to cast it first.
§Examples
use unit_prefix::{NumberPrefix, Prefix};
assert_eq!(
NumberPrefix::binary(1_073_741_824_f64),
NumberPrefix::Prefixed(Prefix::Gibi, 1_f64)
);
Examples found in repository?
examples/conversions.rs (line 36)
29fn main() {
30 // part one, decimal prefixes
31 let mut n = 1_f64;
32 for _ in 0..8 {
33 n *= 1000_f64;
34
35 let decimal = format_prefix(NumberPrefix::decimal(n));
36 let binary = format_prefix(NumberPrefix::binary(n));
37 println!("{:26} bytes is {} and {:10}", n, decimal, binary);
38 }
39
40 println!();
41
42 // part two, binary prefixes
43 let mut n = 1_f64;
44 for _ in 0..8 {
45 n *= 1024_f64;
46
47 let decimal = format_prefix(NumberPrefix::decimal(n));
48 let binary = format_prefix(NumberPrefix::binary(n));
49 println!("{:26} bytes is {} and {:10}", n, binary, decimal);
50 }
51}
Trait Implementations§
Source§impl<F: Clone> Clone for NumberPrefix<F>
impl<F: Clone> Clone for NumberPrefix<F>
Source§fn clone(&self) -> NumberPrefix<F>
fn clone(&self) -> NumberPrefix<F>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<F: Debug> Debug for NumberPrefix<F>
impl<F: Debug> Debug for NumberPrefix<F>
Source§impl<T: FromStr> FromStr for NumberPrefix<T>
impl<T: FromStr> FromStr for NumberPrefix<T>
Source§impl<F: PartialEq> PartialEq for NumberPrefix<F>
impl<F: PartialEq> PartialEq for NumberPrefix<F>
impl<F: Eq> Eq for NumberPrefix<F>
impl<F> StructuralPartialEq for NumberPrefix<F>
Auto Trait Implementations§
impl<F> Freeze for NumberPrefix<F>where
F: Freeze,
impl<F> RefUnwindSafe for NumberPrefix<F>where
F: RefUnwindSafe,
impl<F> Send for NumberPrefix<F>where
F: Send,
impl<F> Sync for NumberPrefix<F>where
F: Sync,
impl<F> Unpin for NumberPrefix<F>where
F: Unpin,
impl<F> UnwindSafe for NumberPrefix<F>where
F: UnwindSafe,
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
Mutably borrows from an owned value. Read more