#[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
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.sign: Sign
§absolute: T
Implementations§
Source§impl Signed<UnsignedDecimal>
impl Signed<UnsignedDecimal>
pub fn new(sign: Sign, absolute: UnsignedDecimal) -> Self
Sourcepub fn try_from_str(s: &str) -> Result<Self, ParseError>
pub fn try_from_str(s: &str) -> Result<Self, ParseError>
Parses a Decimal
.
pub fn try_from_utf8(input_str: &[u8]) -> Result<Self, ParseError>
Sourcepub fn apply_sign_display(&mut self, sign_display: SignDisplay)
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::Decimal;
use fixed_decimal::SignDisplay::*;
let mut dec = Decimal::from(1729);
assert_eq!("1729", dec.to_string());
dec.apply_sign_display(Always);
assert_eq!("+1729", dec.to_string());
Sourcepub fn with_sign_display(self, sign_display: SignDisplay) -> Self
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::Decimal;
use fixed_decimal::SignDisplay::*;
assert_eq!(
"+1729",
Decimal::from(1729)
.with_sign_display(ExceptZero)
.to_string()
);
Source§impl Signed<UnsignedDecimal>
All the rounding and rounding related logic is implmented in this implmentation block.
impl Signed<UnsignedDecimal>
All the rounding and rounding related logic is implmented in this implmentation block.
Sourcepub fn round(&mut self, position: i16)
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::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
dec.round(0);
assert_eq!("-2", dec.to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
dec.round(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
dec.round(0);
assert_eq!("2", dec.to_string());
Sourcepub fn rounded(self, position: i16) -> Self
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::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.rounded(0).to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
assert_eq!("0", dec.rounded(0).to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
assert_eq!("0", dec.rounded(0).to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
assert_eq!("1", dec.rounded(0).to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
assert_eq!("2", dec.rounded(0).to_string());
Sourcepub fn ceil(&mut self, position: i16)
pub fn ceil(&mut self, position: i16)
Rounds this number towards positive infinity at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
dec.ceil(0);
assert_eq!("-1", dec.to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
dec.ceil(0);
assert_eq!("2", dec.to_string());
Sourcepub fn ceiled(self, position: i16) -> Self
pub fn ceiled(self, position: i16) -> Self
Returns this number rounded towards positive infinity at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let dec = Decimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.ceiled(0).to_string());
let dec = Decimal::from_str("0.4").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = Decimal::from_str("0.5").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = Decimal::from_str("0.6").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = Decimal::from_str("1.5").unwrap();
assert_eq!("2", dec.ceiled(0).to_string());
Sourcepub fn expand(&mut self, position: i16)
pub fn expand(&mut self, position: i16)
Rounds this number away from zero at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
dec.expand(0);
assert_eq!("-2", dec.to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
dec.expand(0);
assert_eq!("2", dec.to_string());
Sourcepub fn expanded(self, position: i16) -> Self
pub fn expanded(self, position: i16) -> Self
Returns this number rounded away from zero at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let dec = Decimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.expanded(0).to_string());
let dec = Decimal::from_str("0.4").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = Decimal::from_str("0.5").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = Decimal::from_str("0.6").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = Decimal::from_str("1.5").unwrap();
assert_eq!("2", dec.expanded(0).to_string());
Sourcepub fn floor(&mut self, position: i16)
pub fn floor(&mut self, position: i16)
Rounds this number towards negative infinity at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
dec.floor(0);
assert_eq!("-2", dec.to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
dec.floor(0);
assert_eq!("1", dec.to_string());
Sourcepub fn floored(self, position: i16) -> Self
pub fn floored(self, position: i16) -> Self
Returns this number rounded towards negative infinity at a particular digit position.
§Examples
use fixed_decimal::Decimal;
let dec = Decimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.floored(0).to_string());
let dec = Decimal::from_str("0.4").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = Decimal::from_str("0.5").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = Decimal::from_str("0.6").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = Decimal::from_str("1.5").unwrap();
assert_eq!("1", dec.floored(0).to_string());
Sourcepub fn trunc(&mut self, position: i16)
pub fn trunc(&mut self, position: i16)
Rounds this number towards zero at a particular digit position.
Also see UnsignedDecimal::pad_end()
.
§Examples
use fixed_decimal::Decimal;
let mut dec = Decimal::from_str("-1.5").unwrap();
dec.trunc(0);
assert_eq!("-1", dec.to_string());
let mut dec = Decimal::from_str("0.4").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.5").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("0.6").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = Decimal::from_str("1.5").unwrap();
dec.trunc(0);
assert_eq!("1", dec.to_string());
Sourcepub fn trunced(self, position: i16) -> Self
pub fn trunced(self, position: i16) -> Self
Returns this number rounded towards zero at a particular digit position.
Also see UnsignedDecimal::padded_end()
.
§Examples
use fixed_decimal::Decimal;
let dec = Decimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.trunced(0).to_string());
let dec = Decimal::from_str("0.4").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = Decimal::from_str("0.5").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = Decimal::from_str("0.6").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = Decimal::from_str("1.5").unwrap();
assert_eq!("1", dec.trunced(0).to_string());
Sourcepub fn round_with_mode(&mut self, position: i16, mode: SignedRoundingMode)
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::{Decimal, SignedRoundingMode, UnsignedRoundingMode};
let mut dec = Decimal::from_str("-3.5").unwrap();
dec.round_with_mode(0, SignedRoundingMode::Floor);
assert_eq!("-4", dec.to_string());
let mut dec = Decimal::from_str("-3.5").unwrap();
dec.round_with_mode(0, SignedRoundingMode::Ceil);
assert_eq!("-3", dec.to_string());
let mut dec = Decimal::from_str("5.455").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand),
);
assert_eq!("5.46", dec.to_string());
let mut dec = Decimal::from_str("-7.235").unwrap();
dec.round_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc),
);
assert_eq!("-7.23", dec.to_string());
let mut dec = Decimal::from_str("9.75").unwrap();
dec.round_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven),
);
assert_eq!("9.8", dec.to_string());
Sourcepub fn rounded_with_mode(self, position: i16, mode: SignedRoundingMode) -> Self
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::{Decimal, SignedRoundingMode, UnsignedRoundingMode};
let mut dec = Decimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.rounded_with_mode(0, SignedRoundingMode::Floor)
.to_string()
);
let mut dec = Decimal::from_str("-3.5").unwrap();
assert_eq!(
"-3",
dec.rounded_with_mode(0, SignedRoundingMode::Ceil)
.to_string()
);
let mut dec = Decimal::from_str("5.455").unwrap();
assert_eq!(
"5.46",
dec.rounded_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand)
)
.to_string()
);
let mut dec = Decimal::from_str("-7.235").unwrap();
assert_eq!(
"-7.23",
dec.rounded_with_mode(
-2,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc)
)
.to_string()
);
let mut dec = Decimal::from_str("9.75").unwrap();
assert_eq!(
"9.8",
dec.rounded_with_mode(
-1,
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven)
)
.to_string()
);
Sourcepub fn round_with_mode_and_increment(
&mut self,
position: i16,
mode: SignedRoundingMode,
increment: RoundingIncrement,
)
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::{
Decimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode,
};
let mut dec = Decimal::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 = Decimal::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 = Decimal::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 = Decimal::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 = Decimal::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());
Sourcepub fn rounded_with_mode_and_increment(
self,
position: i16,
mode: SignedRoundingMode,
increment: RoundingIncrement,
) -> Self
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::{
Decimal, RoundingIncrement, SignedRoundingMode, UnsignedRoundingMode,
};
let mut dec = Decimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.rounded_with_mode_and_increment(
0,
SignedRoundingMode::Floor,
RoundingIncrement::MultiplesOf1
)
.to_string()
);
let mut dec = Decimal::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 = Decimal::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 = Decimal::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 = Decimal::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<UnsignedDecimal>
impl Signed<UnsignedDecimal>
Trait Implementations§
Source§impl TryFrom<Signed<UnsignedDecimal>> for FixedInteger
impl TryFrom<Signed<UnsignedDecimal>> for FixedInteger
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> 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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