Struct si_scale::value::Value[][src]

pub struct Value {
    pub mantissa: f64,
    pub prefix: Prefix,
    pub base: Base,
}
Expand description

Represents a float value using its mantissa and unit Prefix in a base.

With base = 1000, 1k = 1000, 1M = 1_000_000, 1m = 0.001, 1µ = 0.000_001, etc.

min (incl.)max (excl.)magnitudeprefix
....-24Prefix::Yocto
....-21Prefix::Zepto
....-18Prefix::Atto
....-15Prefix::Femto
....-12Prefix::Pico
....-9Prefix::Nano
0.000_0010.001-6Prefix::Micro
0.0011-3Prefix::Milli
11_0000Prefix::Unit
10001_000_0003Prefix::Kilo
1_000_0001_000_000_0006Prefix::Mega
....9Prefix::Giga
....12Prefix::Tera
....15Prefix::Peta
....18Prefix::Exa
....21Prefix::Zetta
....24Prefix::Yotta

The base is usually 1000, but can also be 1024 (bibytes).

With base = 1024, 1ki = 1024, 1Mi = 1024 * 1024, etc.

Example

use std::convert::From;
use si_scale::{base::Base, value::Value, prefix::Prefix};

let actual = Value::from(0.123);
let expected = Value {
    mantissa: 123f64,
    prefix: Prefix::Milli,
    base: Base::B1000,
};
assert_eq!(actual, expected);

Fields

mantissa: f64prefix: Prefixbase: Base

Implementations

Returns a Value for the default base B1000, meaning 1 k = 1000, 1 m = 1e-3, etc.

Example
use si_scale::prelude::{Base, Prefix, Value};

let actual = Value::new(-4.6e-5);
let expected = Value {
    mantissa: -46f64,
    prefix: Prefix::Micro,
    base: Base::B1000,
};
assert_eq!(actual, expected);
Note

As always the case in floating point operations, you may encounter approximate representations. For instance:

use si_scale::prelude::{Base, Prefix, Value};

let actual = Value::new(-4.3e-5);
let expected = Value {
    mantissa: -43.00000000000001f64,
    prefix: Prefix::Micro,
    base: Base::B1000,
};
assert_eq!(actual, expected);

Returns a Value for the provided base.

Example
use si_scale::prelude::{Constraint, Base, Prefix, Value};

// 4 MiB
let actual = Value::new_with(4 * 1024 * 1024, Base::B1024, Constraint::None);
let expected = Value {
    mantissa: 4f64,
    prefix: Prefix::Mega,
    base: Base::B1024,
};
assert_eq!(actual, expected);

Converts self to a f64.

Example
use si_scale::prelude::{Base, Prefix, Value};

let value = Value {
    mantissa: 1.3f64,
    prefix: Prefix::Unit,
    base: Base::B1000,
};
assert_eq!(value.to_f64(), 1.3);

Returns a number that represents the sign of self.

  • 1.0 if the number is positive, +0.0 or INFINITY
  • -1.0 if the number is negative, -0.0 or NEG_INFINITY
  • NAN if the number is NAN
Example
use std::convert::From;
use si_scale::value::Value;

let number = -1.5e3f32;
let value: Value = number.into();

assert_eq!(value.signum(), number.signum() as f64);

Trait Implementations

Formats the value using the given formatter. Read more

A basic but limited way to display the value; it does not allow mantissa formatting. Consider using the format_value!() macro instead.

Example
use std::convert::From;
use si_scale::prelude::Value;

let value: Value = 5.3e5.into();

let actual = format!("{}", value);
let expected = "530 k".to_string();
assert_eq!(actual, expected);

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.