pub type Decimal = Signed<UnsignedDecimal>;
Expand description
A Type containing a UnsignedDecimal
and a Sign
to represent a signed decimal number.
Supports a mantissa of non-zero digits and a number of leading and trailing zeros, as well as an optional sign; used for formatting and plural selection.
§Data Types
The following types can be converted to a Decimal
:
- Integers, signed and unsigned
- Strings representing an arbitrary-precision decimal
- Floating point values (using the
ryu
feature)
To create a Decimal
with fractional digits, you have several options:
- Create it from an integer and then call
UnsignedDecimal::multiply_pow10
(you can also callmultiply_pow10
directly on theDecimal
). - Create it from a string.
- When the
ryu
feature is enabled, create it from a floating point value using [Decimal::try_from_f64
].
§Examples
use fixed_decimal::Decimal;
let mut dec = Decimal::from(250);
assert_eq!("250", dec.to_string());
dec.multiply_pow10(-2);
assert_eq!("2.50", dec.to_string());
Aliased Type§
struct Decimal {
pub sign: Sign,
pub absolute: UnsignedDecimal,
}
Fields§
§sign: Sign
§absolute: UnsignedDecimal
Implementations§
Source§impl Decimal
impl Decimal
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 Decimal
All the rounding and rounding related logic is implmented in this implmentation block.
impl Decimal
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()
);
Methods from Deref<Target = UnsignedDecimal>§
Sourcepub fn digit_at(&self, magnitude: i16) -> u8
pub fn digit_at(&self, magnitude: i16) -> u8
Gets the digit at the specified order of magnitude. Returns 0 if the magnitude is out of range of the currently visible digits.
§Examples
use fixed_decimal::UnsignedDecimal;
let dec = UnsignedDecimal::from(945u32);
assert_eq!(0, dec.digit_at(-1));
assert_eq!(5, dec.digit_at(0));
assert_eq!(4, dec.digit_at(1));
assert_eq!(9, dec.digit_at(2));
assert_eq!(0, dec.digit_at(3));
Sourcepub fn magnitude_range(&self) -> RangeInclusive<i16>
pub fn magnitude_range(&self) -> RangeInclusive<i16>
Gets the visible range of digit magnitudes, in ascending order of magnitude. Call .rev()
on the return value to get the range in descending order. Magnitude 0 is always included,
even if the number has leading or trailing zeros.
§Examples
use fixed_decimal::UnsignedDecimal;
let dec: UnsignedDecimal = "012.340".parse().expect("valid syntax");
assert_eq!(-3..=2, dec.magnitude_range());
Sourcepub fn nonzero_magnitude_start(&self) -> i16
pub fn nonzero_magnitude_start(&self) -> i16
Gets the magnitude of the largest nonzero digit. If the number is zero, 0 is returned.
§Examples
use fixed_decimal::UnsignedDecimal;
let dec: UnsignedDecimal = "012.340".parse().expect("valid syntax");
assert_eq!(1, dec.nonzero_magnitude_start());
assert_eq!(0, UnsignedDecimal::from(0u32).nonzero_magnitude_start());
Sourcepub fn nonzero_magnitude_end(&self) -> i16
pub fn nonzero_magnitude_end(&self) -> i16
Gets the magnitude of the smallest nonzero digit. If the number is zero, 0 is returned.
§Examples
use fixed_decimal::UnsignedDecimal;
let dec: UnsignedDecimal = "012.340".parse().expect("valid syntax");
assert_eq!(-2, dec.nonzero_magnitude_end());
assert_eq!(0, UnsignedDecimal::from(0u32).nonzero_magnitude_end());
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns whether the number has a numeric value of zero.
§Examples
use fixed_decimal::UnsignedDecimal;
let dec: UnsignedDecimal = "000.000".parse().expect("valid syntax");
assert!(dec.is_zero());
Sourcepub fn multiply_pow10(&mut self, delta: i16)
pub fn multiply_pow10(&mut self, delta: i16)
Shift the digits of this number by a power of 10.
Leading or trailing zeros may be added to keep the digit at magnitude 0 (the last digit before the decimal separator) visible.
NOTE: if the operation causes overflow, the number will be set to zero.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(42u32);
assert_eq!("42", dec.to_string());
dec.multiply_pow10(3);
assert_eq!("42000", dec.to_string());
Sourcepub fn trim_start(&mut self)
pub fn trim_start(&mut self)
Removes the leading zeroes of this number.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(123400u32)
.multiplied_pow10(-4)
.padded_start(4);
assert_eq!("0012.3400", dec.to_string());
dec.trim_start();
assert_eq!("12.3400", dec.to_string());
There is no effect if the most significant digit has magnitude less than zero
let mut dec = UnsignedDecimal::from(22u32).multiplied_pow10(-4);
assert_eq!("0.0022", dec.to_string());
dec.trim_start();
assert_eq!("0.0022", dec.to_string());
Sourcepub fn trim_end(&mut self)
pub fn trim_end(&mut self)
Removes the trailing zeros of this number.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(123400u32)
.multiplied_pow10(-4)
.padded_start(4);
assert_eq!("0012.3400", dec.to_string());
dec.trim_end();
assert_eq!("0012.34", dec.to_string());
There is no effect if the least significant digit has magnitude more than zero:
let mut dec = UnsignedDecimal::from(2200u32);
assert_eq!("2200", dec.to_string());
dec.trim_end();
assert_eq!("2200", dec.to_string());
Sourcepub fn trim_end_if_integer(&mut self)
pub fn trim_end_if_integer(&mut self)
Removes the trailing zeros of this number, but only if the number is an integer
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(12340000u32).multiplied_pow10(-2);
assert_eq!("123400.00", dec.to_string());
dec.trim_end_if_integer();
assert_eq!("123400", dec.to_string());
// No effect on trailing zeros in the integer:
dec.trim_end_if_integer();
assert_eq!("123400", dec.to_string());
// No effect if there are nonzero fractional digits:
dec.multiply_pow10(-4);
dec.pad_start(4);
assert_eq!("0012.3400", dec.to_string());
dec.trim_end_if_integer();
assert_eq!("0012.3400", dec.to_string());
Sourcepub fn pad_start(&mut self, position: i16)
pub fn pad_start(&mut self, position: i16)
Pads this number with leading zeros on a particular position.
Negative position numbers have no effect.
Also see UnsignedDecimal::set_max_position()
.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(42u32);
assert_eq!("42", dec.to_string());
dec.pad_start(4);
assert_eq!("0042", dec.to_string());
dec.pad_start(3);
assert_eq!("042", dec.to_string());
dec.pad_start(2);
assert_eq!("42", dec.to_string());
dec.pad_start(1);
assert_eq!("42", dec.to_string());
Sourcepub fn pad_end(&mut self, position: i16)
pub fn pad_end(&mut self, position: i16)
Pads this number with trailing zeros on a particular (non-positive) position. Will truncate trailing zeros if necessary, but will not truncate other digits.
Positive position numbers have no effect.
Also see UnsignedDecimal::trunc()
.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from_str("123.456").unwrap();
assert_eq!("123.456", dec.to_string());
dec.pad_end(-2);
assert_eq!("123.456", dec.to_string());
dec.pad_end(-6);
assert_eq!("123.456000", dec.to_string());
let mut dec = UnsignedDecimal::from_str("123.000").unwrap();
dec.pad_end(0);
assert_eq!("123", dec.to_string());
Sourcepub fn set_max_position(&mut self, position: i16)
pub fn set_max_position(&mut self, position: i16)
Truncates the leading significant digits of this number to a particular position, deleting digits if necessary.
Also see UnsignedDecimal::pad_start()
.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut dec = UnsignedDecimal::from(4235970u32).multiplied_pow10(-3);
assert_eq!("4235.970", dec.to_string());
dec.set_max_position(5);
assert_eq!("04235.970", dec.to_string());
dec.set_max_position(2);
assert_eq!("35.970", dec.to_string());
dec.set_max_position(1);
assert_eq!("5.970", dec.to_string());
dec.set_max_position(0);
assert_eq!("0.970", dec.to_string());
dec.set_max_position(-1);
assert_eq!("0.070", dec.to_string());
dec.set_max_position(-2);
assert_eq!("0.000", dec.to_string());
dec.set_max_position(-4);
assert_eq!("0.0000", dec.to_string());
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::UnsignedDecimal;
let mut dec = UnsignedDecimal::from_str("0.4").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.5").unwrap();
dec.round(0);
assert_eq!("0", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.6").unwrap();
dec.round(0);
assert_eq!("1", dec.to_string());
let mut dec = UnsignedDecimal::from_str("1.5").unwrap();
dec.round(0);
assert_eq!("2", dec.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::UnsignedDecimal;
let mut dec = UnsignedDecimal::from_str("0.4").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.5").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.6").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = UnsignedDecimal::from_str("1.5").unwrap();
dec.expand(0);
assert_eq!("2", dec.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::UnsignedDecimal;
let mut dec = UnsignedDecimal::from_str("0.4").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.5").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = UnsignedDecimal::from_str("0.6").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = UnsignedDecimal::from_str("1.5").unwrap();
dec.trunc(0);
assert_eq!("1", dec.to_string());
Sourcepub fn round_with_mode(&mut self, position: i16, mode: UnsignedRoundingMode)
pub fn round_with_mode(&mut self, position: i16, mode: UnsignedRoundingMode)
Rounds this number at a particular digit position, using the specified rounding mode.
§Examples
use fixed_decimal::{UnsignedDecimal, UnsignedRoundingMode};
let mut dec = UnsignedDecimal::from_str("5.455").unwrap();
dec.round_with_mode(-2, UnsignedRoundingMode::HalfExpand);
assert_eq!("5.46", dec.to_string());
let mut dec = UnsignedDecimal::from_str("9.75").unwrap();
dec.round_with_mode(-1, UnsignedRoundingMode::HalfEven);
assert_eq!("9.8", dec.to_string());
Sourcepub fn round_with_mode_and_increment(
&mut self,
position: i16,
mode: UnsignedRoundingMode,
increment: RoundingIncrement,
)
pub fn round_with_mode_and_increment( &mut self, position: i16, mode: UnsignedRoundingMode, increment: RoundingIncrement, )
Rounds this number at a particular digit position and increment, using the specified rounding mode.
§Examples
use fixed_decimal::{
RoundingIncrement, UnsignedDecimal, UnsignedRoundingMode,
};
let mut dec = UnsignedDecimal::from_str("5.455").unwrap();
dec.round_with_mode_and_increment(
-2,
UnsignedRoundingMode::HalfExpand,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("5.45", dec.to_string());
let mut dec = UnsignedDecimal::from_str("9.75").unwrap();
dec.round_with_mode_and_increment(
-1,
UnsignedRoundingMode::HalfEven,
RoundingIncrement::MultiplesOf5,
);
assert_eq!("10.0", dec.to_string());
Sourcepub fn concatenate_end(
&mut self,
other: UnsignedDecimal,
) -> Result<(), UnsignedDecimal>
pub fn concatenate_end( &mut self, other: UnsignedDecimal, ) -> Result<(), UnsignedDecimal>
Concatenate another UnsignedDecimal
into the end of this UnsignedDecimal
.
All nonzero digits in other
must have lower magnitude than nonzero digits in self
.
If the two decimals represent overlapping ranges of magnitudes, an Err
is returned,
passing ownership of other
back to the caller.
The magnitude range of self
will be increased if other
covers a larger range.
§Examples
use fixed_decimal::UnsignedDecimal;
let mut integer = UnsignedDecimal::from(123u32);
let fraction = UnsignedDecimal::from(456u32).multiplied_pow10(-3);
integer.concatenate_end(fraction);
assert_eq!("123.456", integer.to_string());
Trait Implementations§
Source§impl Display for Decimal
This trait is implemented for compatibility with fmt!
.
To create a string, [Writeable::write_to_string
] is usually more efficient.
impl Display for Decimal
This trait is implemented for compatibility with fmt!
.
To create a string, [Writeable::write_to_string
] is usually more efficient.
Source§impl From<FixedInteger> for Decimal
impl From<FixedInteger> for Decimal
Source§fn from(value: FixedInteger) -> Self
fn from(value: FixedInteger) -> Self
Source§impl Writeable for Decimal
Render the Decimal
as a string of ASCII digits with a possible decimal point.
impl Writeable for Decimal
Render the Decimal
as a string of ASCII digits with a possible decimal point.
§Examples
assert_writeable_eq!(Decimal::from(42), "42");
Source§fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
write_to_parts
, and discards any
Part
annotations.Source§fn writeable_length_hint(&self) -> LengthHint
fn writeable_length_hint(&self) -> LengthHint
Source§fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
S: PartsWrite + ?Sized,
fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
S: PartsWrite + ?Sized,
Part
annotations to the given sink. Errors from the
sink are bubbled up. The default implementation delegates to write_to
,
and doesn’t produce any Part
annotations.