Struct fixed_decimal::Signed

source ·
#[non_exhaustive]
pub struct Signed<T> { pub sign: Sign, pub absolute: T, }
Expand description

The Signed struct represents a numeric value with an associated sign.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§sign: Sign§absolute: T

Implementations§

source§

impl Signed<UnsignedFixedDecimal>

source

pub fn new(sign: Sign, absolute: UnsignedFixedDecimal) -> Self

source

pub fn try_from_str(s: &str) -> Result<Self, ParseError>

source

pub fn try_from_utf8(input_str: &[u8]) -> Result<Self, ParseError>

source

pub fn apply_sign_display(&mut self, sign_display: SignDisplay)

Sets the sign of this number according to the given sign display strategy.

§Examples
use fixed_decimal::SignedFixedDecimal;
use fixed_decimal::SignDisplay::*;

let mut dec = SignedFixedDecimal::from(1729);
assert_eq!("1729", dec.to_string());
dec.apply_sign_display(Always);
assert_eq!("+1729", dec.to_string());
source

pub fn with_sign_display(self, sign_display: SignDisplay) -> Self

Returns this number with its sign set according to the given sign display strategy.

§Examples
use fixed_decimal::SignedFixedDecimal;
use fixed_decimal::SignDisplay::*;

assert_eq!(
    "+1729",
    SignedFixedDecimal::from(1729)
        .with_sign_display(ExceptZero)
        .to_string()
);
source§

impl Signed<UnsignedFixedDecimal>

source

pub fn try_from_f64( float: f64, precision: FloatPrecision, ) -> Result<Self, LimitError>

Constructs a SignedFixedDecimal from an f64.

Since f64 values do not carry a notion of their precision, the second argument to this function specifies the type of precision associated with the f64. For more information, see FloatPrecision.

This function uses ryu, which is an efficient double-to-string algorithm, but other implementations may yield higher performance; for more details, see icu4x#166.

This function can be made available with the "ryu" Cargo feature.

use fixed_decimal::{SignedFixedDecimal, FloatPrecision};
use writeable::assert_writeable_eq;

let decimal =
    SignedFixedDecimal::try_from_f64(-5.1, FloatPrecision::Magnitude(-2))
        .expect("Finite quantity with limited precision");
assert_writeable_eq!(decimal, "-5.10");

let decimal =
    SignedFixedDecimal::try_from_f64(0.012345678, FloatPrecision::RoundTrip)
        .expect("Finite quantity");
assert_writeable_eq!(decimal, "0.012345678");

let decimal =
    SignedFixedDecimal::try_from_f64(12345678000., FloatPrecision::Integer)
        .expect("Finite, integer-valued quantity");
assert_writeable_eq!(decimal, "12345678000");

Negative zero is supported.

use fixed_decimal::{SignedFixedDecimal, FloatPrecision};
use writeable::assert_writeable_eq;

// IEEE 754 for floating point defines the sign bit separate
// from the mantissa and exponent, allowing for -0.
let negative_zero =
    SignedFixedDecimal::try_from_f64(-0.0, FloatPrecision::Integer)
        .expect("Negative zero");
assert_writeable_eq!(negative_zero, "-0");
source§

impl Signed<UnsignedFixedDecimal>

All the rounding and rounding related logic is implmented in this implmentation block.

source

pub fn round(&mut self, position: i16)

Rounds this number at a particular digit position.

This uses half to even rounding, which rounds to the nearest integer and resolves ties by selecting the nearest even integer to the original value.

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
dec.round(0);
assert_eq!("-2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
dec.round(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
dec.round(0);
assert_eq!("2", dec.to_string());
source

pub fn rounded(self, position: i16) -> Self

Returns this number rounded at a particular digit position.

This uses half to even rounding by default, which rounds to the nearest integer and resolves ties by selecting the nearest even integer to the original value.

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.rounded(0).to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.rounded(0).to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.rounded(0).to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.rounded(0).to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.rounded(0).to_string());
source

pub fn ceil(&mut self, position: i16)

Rounds this number towards positive infinity at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
dec.ceil(0);
assert_eq!("-1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
dec.ceil(0);
assert_eq!("2", dec.to_string());
source

pub fn ceiled(self, position: i16) -> Self

Returns this number rounded towards positive infinity at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let dec = SignedFixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.ceiled(0).to_string());
let dec = SignedFixedDecimal::from_str("0.4").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = SignedFixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = SignedFixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = SignedFixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.ceiled(0).to_string());
source

pub fn expand(&mut self, position: i16)

Rounds this number away from zero at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
dec.expand(0);
assert_eq!("-2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
dec.expand(0);
assert_eq!("2", dec.to_string());
source

pub fn expanded(self, position: i16) -> Self

Returns this number rounded away from zero at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let dec = SignedFixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.expanded(0).to_string());
let dec = SignedFixedDecimal::from_str("0.4").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = SignedFixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = SignedFixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = SignedFixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.expanded(0).to_string());
source

pub fn floor(&mut self, position: i16)

Rounds this number towards negative infinity at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
dec.floor(0);
assert_eq!("-2", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
dec.floor(0);
assert_eq!("1", dec.to_string());
source

pub fn floored(self, position: i16) -> Self

Returns this number rounded towards negative infinity at a particular digit position.

§Examples
use fixed_decimal::SignedFixedDecimal;

let dec = SignedFixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.floored(0).to_string());
let dec = SignedFixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = SignedFixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = SignedFixedDecimal::from_str("0.6").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = SignedFixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.floored(0).to_string());
source

pub fn trunc(&mut self, position: i16)

Rounds this number towards zero at a particular digit position.

Also see UnsignedFixedDecimal::pad_end().

§Examples
use fixed_decimal::SignedFixedDecimal;

let mut dec = SignedFixedDecimal::from_str("-1.5").unwrap();
dec.trunc(0);
assert_eq!("-1", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.4").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.5").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("0.6").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("1.5").unwrap();
dec.trunc(0);
assert_eq!("1", dec.to_string());
source

pub fn trunced(self, position: i16) -> Self

Returns this number rounded towards zero at a particular digit position.

Also see UnsignedFixedDecimal::padded_end().

§Examples
use fixed_decimal::SignedFixedDecimal;

let dec = SignedFixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.trunced(0).to_string());
let dec = SignedFixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = SignedFixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = SignedFixedDecimal::from_str("0.6").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = SignedFixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.trunced(0).to_string());
source

pub fn round_with_mode(&mut self, position: i16, mode: SignedRoundingMode)

Rounds this number at a particular digit position, using the specified rounding mode.

§Examples
use fixed_decimal::{SignedFixedDecimal, SignedRoundingMode, UnsignedRoundingMode};

let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
dec.round_with_mode(0, SignedRoundingMode::Floor);
assert_eq!("-4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
dec.round_with_mode(0, SignedRoundingMode::Ceil);
assert_eq!("-3", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5.455").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand));
assert_eq!("5.46", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-7.235").unwrap();
dec.round_with_mode(-2, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc));
assert_eq!("-7.23", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("9.75").unwrap();
dec.round_with_mode(-1, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven));
assert_eq!("9.8", dec.to_string());
source

pub fn rounded_with_mode(self, position: i16, mode: SignedRoundingMode) -> Self

Returns this number rounded at a particular digit position, using the specified rounding mode.

§Examples
use fixed_decimal::{SignedFixedDecimal, SignedRoundingMode, UnsignedRoundingMode};

let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
    "-4",
    dec.rounded_with_mode(0, SignedRoundingMode::Floor).to_string()
);
let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
    "-3",
    dec.rounded_with_mode(0, SignedRoundingMode::Ceil).to_string()
);
let mut dec = SignedFixedDecimal::from_str("5.455").unwrap();
assert_eq!(
    "5.46",
    dec.rounded_with_mode(-2, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand))
        .to_string()
);
let mut dec = SignedFixedDecimal::from_str("-7.235").unwrap();
assert_eq!(
    "-7.23",
    dec.rounded_with_mode(-2, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc))
        .to_string()
);
let mut dec = SignedFixedDecimal::from_str("9.75").unwrap();
assert_eq!(
    "9.8",
    dec.rounded_with_mode(-1, SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven))
        .to_string()
);
source

pub fn round_with_mode_and_increment( &mut self, position: i16, mode: SignedRoundingMode, increment: RoundingIncrement, )

Rounds this number at a particular digit position and increment, using the specified rounding mode.

§Examples
use fixed_decimal::{SignedFixedDecimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode};

let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
dec.round_with_mode_and_increment(
    0,
    SignedRoundingMode::Floor,
    RoundingIncrement::MultiplesOf1,
);
assert_eq!("-4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-3.59").unwrap();
dec.round_with_mode_and_increment(
    -1,
    SignedRoundingMode::Ceil,
    RoundingIncrement::MultiplesOf2,
);
assert_eq!("-3.4", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("5.455").unwrap();
dec.round_with_mode_and_increment(
    -2,
    SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
    RoundingIncrement::MultiplesOf5,
);
assert_eq!("5.45", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("-7.235").unwrap();
dec.round_with_mode_and_increment(
    -2,
    SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
    RoundingIncrement::MultiplesOf25,
);
assert_eq!("-7.25", dec.to_string());
let mut dec = SignedFixedDecimal::from_str("9.75").unwrap();
dec.round_with_mode_and_increment(
    -1,
    SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
    RoundingIncrement::MultiplesOf5,
);
assert_eq!("10.0", dec.to_string());
source

pub fn rounded_with_mode_and_increment( self, position: i16, mode: SignedRoundingMode, increment: RoundingIncrement, ) -> Self

Returns this number rounded at a particular digit position and increment, using the specified rounding mode.

§Examples
use fixed_decimal::{SignedFixedDecimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode};

let mut dec = SignedFixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
    "-4",
    dec.rounded_with_mode_and_increment(
        0,
        SignedRoundingMode::Floor,
        RoundingIncrement::MultiplesOf1
    )
    .to_string()
);
let mut dec = SignedFixedDecimal::from_str("-3.59").unwrap();
assert_eq!(
    "-3.4",
    dec.rounded_with_mode_and_increment(
        -1,
        SignedRoundingMode::Ceil,
        RoundingIncrement::MultiplesOf2
    )
    .to_string()
);
let mut dec = SignedFixedDecimal::from_str("5.455").unwrap();
assert_eq!(
    "5.45",
    dec.rounded_with_mode_and_increment(
        -2,
        SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
        RoundingIncrement::MultiplesOf5
    )
    .to_string()
);
let mut dec = SignedFixedDecimal::from_str("-7.235").unwrap();
assert_eq!(
    "-7.25",
    dec.rounded_with_mode_and_increment(
        -2,
        SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
        RoundingIncrement::MultiplesOf25
    )
    .to_string()
);
let mut dec = SignedFixedDecimal::from_str("9.75").unwrap();
assert_eq!(
    "10.0",
    dec.rounded_with_mode_and_increment(
        -1,
        SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
        RoundingIncrement::MultiplesOf5
    )
    .to_string()
);
source§

impl Signed<UnsignedFixedDecimal>

source

pub fn to_string(&self) -> String

Converts the given value to a String.

Under the hood, this uses an efficient [Writeable] implementation. However, in order to avoid allocating a string, it is more efficient to use [Writeable] directly.

source§

impl<T> Signed<T>

source

pub fn sign(&self) -> Sign

Returns the sign of this signed number.

source

pub fn set_sign(&mut self, sign: Sign)

Changes the sign of this signed number to the one given.

source

pub fn with_sign(self, sign: Sign) -> Self

Returns this number with the sign changed to the one given.

Trait Implementations§

source§

impl<T: Clone> Clone for Signed<T>

source§

fn clone(&self) -> Signed<T>

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<T: Debug> Debug for Signed<T>

source§

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

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

impl<T: Default> Default for Signed<T>

source§

fn default() -> Signed<T>

Returns the “default value” for a type. Read more
source§

impl<T: PartialEq> PartialEq for Signed<T>

source§

fn eq(&self, other: &Signed<T>) -> bool

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

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

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

impl TryFrom<Signed<UnsignedFixedDecimal>> for FixedInteger

source§

type Error = LimitError

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

fn try_from(signed_fd: SignedFixedDecimal) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T> StructuralPartialEq for Signed<T>

Auto Trait Implementations§

§

impl<T> Freeze for Signed<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Signed<T>
where T: RefUnwindSafe,

§

impl<T> Send for Signed<T>
where T: Send,

§

impl<T> Sync for Signed<T>
where T: Sync,

§

impl<T> Unpin for Signed<T>
where T: Unpin,

§

impl<T> UnwindSafe for Signed<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

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 T
where 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 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.