Struct fixed_decimal::decimal::FixedDecimal
source · pub struct FixedDecimal {
digits: SmallVec<[u8; 8]>,
magnitude: i16,
upper_magnitude: i16,
lower_magnitude: i16,
sign: Sign,
}
Expand description
A struct containing decimal digits with efficient iteration and manipulation by magnitude (power of 10). 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 FixedDecimal
:
- Integers, signed and unsigned
- Strings representing an arbitrary-precision decimal
To create a FixedDecimal
with fraction digits, either create it from an integer and then
call FixedDecimal::multiplied_pow10
, or create it from a string.
Floating point numbers will be supported pending a resolution to #166. In the mean time, a third-party float-to-string library may be used.
§Magnitude and Position
Each digit in a FixedDecimal
is indexed by a magnitude, or the digit’s power of 10.
Illustration for the number “12.34”:
Magnitude | Digit | Description |
---|---|---|
1 | 1 | Tens place |
0 | 2 | Ones place |
-1 | 3 | Tenths place |
-2 | 4 | Hundredths place |
Some functions deal with a position for the purpose of padding, truncating, or rounding a number. In these cases, the position is the index on the right side of the digit of the corresponding magnitude. Illustration:
Position: 2 0 -2
Number: |1|2.3|4|
Position: 1 -1
Expected output of various operations, all with input “12.34”:
Operation | Position | Expected Result |
---|---|---|
Truncate Left | 1 | 10 |
Truncate Right | -1 | 12.3 |
Pad Left | 4 | 0012.34 |
Pad Right | -4 | 12.3400 |
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(250);
assert_eq!("250", dec.to_string());
dec.multiply_pow10(-2);
assert_eq!("2.50", dec.to_string());
Fields§
§digits: SmallVec<[u8; 8]>
List of digits; digits[0] is the most significant.
Invariants:
- Must not include leading or trailing zeros
- Length must not exceed (magnitude - lower_magnitude + 1)
magnitude: i16
Power of 10 of digits[0].
Invariants:
- <= upper_magnitude
-
= lower_magnitude
upper_magnitude: i16
Power of 10 of the most significant digit, which may be zero.
Invariants:
-
= 0
-
= magnitude
lower_magnitude: i16
Power of 10 of the least significant digit, which may be zero.
Invariants:
- <= 0
- <= magnitude
sign: Sign
The sign; note that a positive value may be represented by either
Sign::Positive
(corresponding to a prefix +) or Sign::None
(corresponding to the absence of a prefix sign).
Implementations§
source§impl FixedDecimal
impl FixedDecimal
sourcefn from_ascending<T>(digits_iter: T) -> Result<Self, Error>
fn from_ascending<T>(digits_iter: T) -> Result<Self, Error>
Initialize a FixedDecimal
with an iterator of digits in ascending
order of magnitude, starting with the digit at magnitude 0.
This method is not public; use TryFrom::<isize>
instead.
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::FixedDecimal;
let dec = FixedDecimal::from(945);
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));
sourcefn digit_at_previous_position(&self, magnitude: i16) -> u8
fn digit_at_previous_position(&self, magnitude: i16) -> u8
Gets the digit at the specified order of next upper magnitude (magnitude + 1).
Returns 0 if the next upper magnitude is out of range of currently visible digits or
the magnitude is equal to i16::MAX
.
sourcefn digit_at_next_position(&self, magnitude: i16) -> u8
fn digit_at_next_position(&self, magnitude: i16) -> u8
Gets the digit at the specified order of next lower magnitude (magnitude - 1).
Returns 0 if the next lower magnitude is out of range of currently visible digits or the
magnitude is equal to i16::MIN
.
sourcefn half_at_next_magnitude(&self, magnitude: i16) -> Ordering
fn half_at_next_magnitude(&self, magnitude: i16) -> Ordering
Returns the relative ordering of the digits after magnitude
with respect to 5.
sourcefn half_increment_at_magnitude<R: IncrementLike>(
&self,
magnitude: i16,
increment: R,
) -> Ordering
fn half_increment_at_magnitude<R: IncrementLike>( &self, magnitude: i16, increment: R, ) -> Ordering
Returns the relative ordering of the digits from magnitude
onwards
with respect to the half increment of increment
.
sourcefn is_rounded<R: IncrementLike>(&self, position: i16, increment: R) -> bool
fn is_rounded<R: IncrementLike>(&self, position: i16, increment: R) -> bool
Checks if this number is already rounded to the specified magnitude and increment.
sourcepub const fn magnitude_range(&self) -> RangeInclusive<i16>
pub const 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::FixedDecimal;
let dec: FixedDecimal = "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::FixedDecimal;
let dec: FixedDecimal = "012.340".parse().expect("valid syntax");
assert_eq!(1, dec.nonzero_magnitude_start());
assert_eq!(0, FixedDecimal::from(0).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::FixedDecimal;
let dec: FixedDecimal = "012.340".parse().expect("valid syntax");
assert_eq!(-2, dec.nonzero_magnitude_end());
assert_eq!(0, FixedDecimal::from(0).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::FixedDecimal;
let dec: FixedDecimal = "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 by a power of 10, modifying self.
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::FixedDecimal;
let mut dec = FixedDecimal::from(42);
assert_eq!("42", dec.to_string());
dec.multiply_pow10(3);
assert_eq!("42000", dec.to_string());
sourcepub fn multiplied_pow10(self, delta: i16) -> Self
pub fn multiplied_pow10(self, delta: i16) -> Self
Shift the digits by a power of 10, consuming self and returning a new object if successful.
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 returned number will be zero.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from(42).multiplied_pow10(3);
assert_eq!("42000", dec.to_string());
sourcepub fn sign(&self) -> Sign
pub fn sign(&self) -> Sign
Returns the sign.
§Examples
use fixed_decimal::FixedDecimal;
use fixed_decimal::Sign;
assert_eq!(FixedDecimal::from_str("1729").unwrap().sign(), Sign::None);
assert_eq!(
FixedDecimal::from_str("-1729").unwrap().sign(),
Sign::Negative
);
assert_eq!(
FixedDecimal::from_str("+1729").unwrap().sign(),
Sign::Positive
);
sourcepub fn set_sign(&mut self, sign: Sign)
pub fn set_sign(&mut self, sign: Sign)
Change the sign to the one given.
§Examples
use fixed_decimal::FixedDecimal;
use fixed_decimal::Sign;
let mut dec = FixedDecimal::from(1729);
assert_eq!("1729", dec.to_string());
dec.set_sign(Sign::Negative);
assert_eq!("-1729", dec.to_string());
dec.set_sign(Sign::Positive);
assert_eq!("+1729", dec.to_string());
dec.set_sign(Sign::None);
assert_eq!("1729", dec.to_string());
sourcepub fn with_sign(self, sign: Sign) -> Self
pub fn with_sign(self, sign: Sign) -> Self
Change the sign to the one given, consuming self and returning a new object.
§Examples
use fixed_decimal::FixedDecimal;
use fixed_decimal::Sign;
assert_eq!(
"+1729",
FixedDecimal::from(1729)
.with_sign(Sign::Positive)
.to_string()
);
assert_eq!(
"1729",
FixedDecimal::from(-1729).with_sign(Sign::None).to_string()
);
assert_eq!(
"-1729",
FixedDecimal::from(1729)
.with_sign(Sign::Negative)
.to_string()
);
sourcepub fn apply_sign_display(&mut self, sign_display: SignDisplay)
pub fn apply_sign_display(&mut self, sign_display: SignDisplay)
Sets the sign according to the given sign display strategy.
§Examples
use fixed_decimal::FixedDecimal;
use fixed_decimal::SignDisplay::*;
let mut dec = FixedDecimal::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
Sets the sign according to the given sign display strategy, consuming self and returning a new object.
§Examples
use fixed_decimal::FixedDecimal;
use fixed_decimal::SignDisplay::*;
assert_eq!(
"+1729",
FixedDecimal::from(1729)
.with_sign_display(ExceptZero)
.to_string()
);
sourcepub fn trimmed_start(self) -> Self
pub fn trimmed_start(self) -> Self
Remove leading zeroes, consuming self and returning a new object.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from(123400)
.multiplied_pow10(-4)
.padded_start(4);
assert_eq!("0012.3400", dec.to_string());
assert_eq!("12.3400", dec.trimmed_start().to_string());
sourcepub fn trim_start(&mut self)
pub fn trim_start(&mut self)
Remove leading zeroes, modifying self.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(123400)
.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 = FixedDecimal::from(22).multiplied_pow10(-4);
assert_eq!("0.0022", dec.to_string());
dec.trim_start();
assert_eq!("0.0022", dec.to_string());
sourcepub fn trimmed_end(self) -> Self
pub fn trimmed_end(self) -> Self
Remove trailing zeroes, consuming self and returning a new object.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from(123400)
.multiplied_pow10(-4)
.padded_start(4);
assert_eq!("0012.3400", dec.to_string());
assert_eq!("0012.34", dec.trimmed_end().to_string());
sourcepub fn trim_end(&mut self)
pub fn trim_end(&mut self)
Remove trailing zeroes, modifying self.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(123400)
.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 = FixedDecimal::from(2200);
assert_eq!("2200", dec.to_string());
dec.trim_end();
assert_eq!("2200", dec.to_string());
sourcepub fn padded_start(self, position: i16) -> Self
pub fn padded_start(self, position: i16) -> Self
Zero-pad the number on the left to a particular position, returning the result.
Negative numbers have no effect.
Also see FixedDecimal::with_max_position()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(42);
assert_eq!("42", dec.to_string());
assert_eq!("0042", dec.clone().padded_start(4).to_string());
assert_eq!("042", dec.clone().padded_start(3).to_string());
assert_eq!("42", dec.clone().padded_start(2).to_string());
assert_eq!("42", dec.clone().padded_start(1).to_string());
sourcepub fn pad_start(&mut self, position: i16)
pub fn pad_start(&mut self, position: i16)
Zero-pad the number on the left to a particular position.
Negative numbers have no effect.
Also see FixedDecimal::set_max_position()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(42);
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 padded_end(self, position: i16) -> Self
pub fn padded_end(self, position: i16) -> Self
Zero-pad the number on the right to a particular (negative) position. Will truncate trailing zeros if necessary, but will not truncate other digits, returning the result.
Positive numbers have no effect.
Also see FixedDecimal::trunced()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("123.456").unwrap();
assert_eq!("123.456", dec.to_string());
assert_eq!("123.456", dec.clone().padded_end(-1).to_string());
assert_eq!("123.456", dec.clone().padded_end(-2).to_string());
assert_eq!("123.456000", dec.clone().padded_end(-6).to_string());
assert_eq!("123.4560", dec.clone().padded_end(-4).to_string());
sourcepub fn pad_end(&mut self, position: i16)
pub fn pad_end(&mut self, position: i16)
Zero-pad the number on the right to a particular (negative) position. Will truncate trailing zeros if necessary, but will not truncate other digits.
Positive numbers have no effect.
Also see FixedDecimal::trunc()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("123.456").unwrap();
assert_eq!("123.456", dec.to_string());
dec.pad_end(-1);
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());
dec.pad_end(-4);
assert_eq!("123.4560", dec.to_string());
sourcepub fn with_max_position(self, position: i16) -> Self
pub fn with_max_position(self, position: i16) -> Self
Truncate the number on the left to a particular position, deleting digits if necessary, returning the result.
Also see FixedDecimal::padded_start()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(4235970).multiplied_pow10(-3);
assert_eq!("4235.970", dec.to_string());
assert_eq!("04235.970", dec.clone().with_max_position(5).to_string());
assert_eq!("35.970", dec.clone().with_max_position(2).to_string());
assert_eq!("5.970", dec.clone().with_max_position(1).to_string());
assert_eq!("0.970", dec.clone().with_max_position(0).to_string());
assert_eq!("0.070", dec.clone().with_max_position(-1).to_string());
assert_eq!("0.000", dec.clone().with_max_position(-2).to_string());
assert_eq!("0.0000", dec.clone().with_max_position(-4).to_string());
sourcepub fn set_max_position(&mut self, position: i16)
pub fn set_max_position(&mut self, position: i16)
Truncate the number on the left to a particular position, deleting digits if necessary.
Also see FixedDecimal::pad_start()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from(4235970).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 trunc(&mut self, position: i16)
pub fn trunc(&mut self, position: i16)
Truncates the number on the right to a particular position, deleting digits if necessary.
Also see FixedDecimal::pad_end()
.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.trunc(0);
assert_eq!("-1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.trunc(0);
assert_eq!("1", dec.to_string());
sourcepub fn trunc_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn trunc_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Truncates the number on the right to a particular position and rounding increment, deleting digits if necessary.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.trunc_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.trunc_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("5.45").unwrap();
dec.trunc_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("5.25", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.trunc_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.trunc_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("9.98", dec.to_string());
fn trunc_to_increment_internal<R: IncrementLike>( &mut self, position: i16, inner_increment: R, )
sourcepub fn trunced(self, position: i16) -> Self
pub fn trunced(self, position: i16) -> Self
Truncate the number on the right to a particular position, deleting digits if necessary.
Also see FixedDecimal::padded_end()
.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.trunced(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("0", dec.trunced(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.trunced(0).to_string());
sourcepub fn trunced_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn trunced_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Truncates the number on the right to a particular position and rounding increment, deleting digits if necessary.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-2",
dec.trunced_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.trunced_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("5.45").unwrap();
assert_eq!(
"5.25",
dec.trunced_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"7.5",
dec.trunced_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"9.98",
dec.trunced_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn half_trunc(&mut self, position: i16)
pub fn half_trunc(&mut self, position: i16)
Half Truncates the number on the right to a particular position, deleting digits if necessary.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.half_trunc(0);
assert_eq!("-1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.half_trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.half_trunc(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.half_trunc(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.half_trunc(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("3.954").unwrap();
dec.half_trunc(0);
assert_eq!("4", dec.to_string());
sourcepub fn half_trunc_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn half_trunc_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Half Truncates the number on the right to a particular position and rounding increment, deleting digits if necessary.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.half_trunc_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.half_trunc_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("5.45").unwrap();
dec.half_trunc_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_trunc_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_trunc_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("9.98", dec.to_string());
fn half_trunc_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn half_trunced(self, position: i16) -> Self
pub fn half_trunced(self, position: i16) -> Self
Half Truncates the number on the right to a particular position, deleting digits if necessary.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.half_trunced(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.half_trunced(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.half_trunced(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.half_trunced(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.half_trunced(0).to_string());
let dec = FixedDecimal::from_str("3.954").unwrap();
assert_eq!("4", dec.half_trunced(0).to_string());
sourcepub fn half_trunced_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn half_trunced_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Half Truncates the number on the right to a particular position and rounding increment, deleting digits if necessary.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.half_trunced_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.half_trunced_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("5.45").unwrap();
assert_eq!(
"5.50",
dec.half_trunced_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.half_trunced_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"9.98",
dec.half_trunced_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn expand(&mut self, position: i16)
pub fn expand(&mut self, position: i16)
Take the expand of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.expand(0);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.expand(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.expand(0);
assert_eq!("2", dec.to_string());
sourcepub fn expand_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn expand_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the expand of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.expand_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.expand_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("8.0", dec.to_string());
let mut dec = FixedDecimal::from_str("5.45").unwrap();
dec.expand_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.expand_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.expand_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("10.00", dec.to_string());
fn expand_to_increment_internal<R: IncrementLike>( &mut self, position: i16, inner_increment: R, )
sourcepub fn expanded(self, position: i16) -> Self
pub fn expanded(self, position: i16) -> Self
Take the expand of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.expanded(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.expanded(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.expanded(0).to_string());
sourcepub fn expanded_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn expanded_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the expand of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.expanded_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"8.0",
dec.expanded_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("5.45").unwrap();
assert_eq!(
"5.50",
dec.expanded_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.expanded_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.00",
dec.expanded_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn half_expand(&mut self, position: i16)
pub fn half_expand(&mut self, position: i16)
Take the half expand of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.half_expand(0);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.half_expand(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.half_expand(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.half_expand(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.half_expand(0);
assert_eq!("2", dec.to_string());
sourcepub fn half_expand_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn half_expand_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the half expand of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.half_expand_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.half_expand_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("5.45").unwrap();
dec.half_expand_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_expand_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_expand_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("10.00", dec.to_string());
fn half_expand_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn half_expanded(self, position: i16) -> Self
pub fn half_expanded(self, position: i16) -> Self
Take the half expand of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.half_expanded(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.half_expanded(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.half_expanded(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.half_expanded(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.half_expanded(0).to_string());
sourcepub fn half_expanded_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn half_expanded_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the half expand of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.half_expanded_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.half_expanded_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("5.45").unwrap();
assert_eq!(
"5.50",
dec.half_expanded_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.half_expanded_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.00",
dec.half_expanded_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn ceil(&mut self, position: i16)
pub fn ceil(&mut self, position: i16)
Take the ceiling of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.ceil(0);
assert_eq!("-1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.ceil(0);
assert_eq!("2", dec.to_string());
sourcepub fn ceil_to_increment(&mut self, position: i16, increment: RoundingIncrement)
pub fn ceil_to_increment(&mut self, position: i16, increment: RoundingIncrement)
Take the ceiling of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.ceil_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.ceil_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("8.0", dec.to_string());
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
dec.ceil_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("-5.25", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.ceil_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
dec.ceil_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("-9.98", dec.to_string());
fn ceil_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn ceiled(self, position: i16) -> Self
pub fn ceiled(self, position: i16) -> Self
Take the ceiling of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.ceiled(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.ceiled(0).to_string());
sourcepub fn ceiled_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn ceiled_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the ceiling of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-2",
dec.ceiled_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"8.0",
dec.ceiled_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
assert_eq!(
"-5.25",
dec.ceiled_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.ceiled_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
assert_eq!(
"-9.98",
dec.ceiled_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn half_ceil(&mut self, position: i16)
pub fn half_ceil(&mut self, position: i16)
Take the half ceiling of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.half_ceil(0);
assert_eq!("-1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.half_ceil(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.half_ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.half_ceil(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.half_ceil(0);
assert_eq!("2", dec.to_string());
sourcepub fn half_ceil_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn half_ceil_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the half ceiling of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.half_ceil_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.half_ceil_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
dec.half_ceil_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("-5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_ceil_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
dec.half_ceil_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("-9.98", dec.to_string());
fn half_ceil_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn half_ceiled(self, position: i16) -> Self
pub fn half_ceiled(self, position: i16) -> Self
Take the half ceiling of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-1", dec.half_ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.half_ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("1", dec.half_ceiled(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.half_ceiled(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.half_ceiled(0).to_string());
sourcepub fn half_ceiled_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn half_ceiled_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the half ceiling of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.half_ceiled_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.half_ceiled_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
assert_eq!(
"-5.50",
dec.half_ceiled_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.half_ceiled_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
assert_eq!(
"-9.98",
dec.half_ceiled_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn floor(&mut self, position: i16)
pub fn floor(&mut self, position: i16)
Take the floor of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.floor(0);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.floor(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.floor(0);
assert_eq!("1", dec.to_string());
sourcepub fn floor_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn floor_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the floor of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.floor_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.floor_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
dec.floor_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("-5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.floor_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
dec.floor_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("-10.00", dec.to_string());
fn floor_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn floored(self, position: i16) -> Self
pub fn floored(self, position: i16) -> Self
Take the floor of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.floored(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("0", dec.floored(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.floored(0).to_string());
sourcepub fn floored_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn floored_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the floor of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.floored_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.floored_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
assert_eq!(
"-5.50",
dec.floored_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"7.5",
dec.floored_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
assert_eq!(
"-10.00",
dec.floored_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn half_floor(&mut self, position: i16)
pub fn half_floor(&mut self, position: i16)
Take the half floor of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.half_floor(0);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.half_floor(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.half_floor(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.half_floor(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.half_floor(0);
assert_eq!("1", dec.to_string());
sourcepub fn half_floor_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn half_floor_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the half floor of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.half_floor_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.half_floor_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
dec.half_floor_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("-5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_floor_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
dec.half_floor_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("-10.00", dec.to_string());
fn half_floor_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn half_floored(self, position: i16) -> Self
pub fn half_floored(self, position: i16) -> Self
Take the half floor of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.half_floored(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.half_floored(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.half_floored(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.half_floored(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("1", dec.half_floored(0).to_string());
sourcepub fn half_floored_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn half_floored_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the half floor of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.half_floored_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.half_floored_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("-5.45").unwrap();
assert_eq!(
"-5.50",
dec.half_floored_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.half_floored_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("-9.99").unwrap();
assert_eq!(
"-10.00",
dec.half_floored_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn half_even(&mut self, position: i16)
pub fn half_even(&mut self, position: i16)
Take the half even of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let mut dec = FixedDecimal::from_str("-1.5").unwrap();
dec.half_even(0);
assert_eq!("-2", dec.to_string());
let mut dec = FixedDecimal::from_str("0.4").unwrap();
dec.half_even(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.5").unwrap();
dec.half_even(0);
assert_eq!("0", dec.to_string());
let mut dec = FixedDecimal::from_str("0.6").unwrap();
dec.half_even(0);
assert_eq!("1", dec.to_string());
let mut dec = FixedDecimal::from_str("1.5").unwrap();
dec.half_even(0);
assert_eq!("2", dec.to_string());
sourcepub fn half_even_to_increment(
&mut self,
position: i16,
increment: RoundingIncrement,
)
pub fn half_even_to_increment( &mut self, position: i16, increment: RoundingIncrement, )
Take the half even of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
dec.half_even_to_increment(0, RoundingIncrement::MultiplesOf2);
assert_eq!("-4", dec.to_string());
let mut dec = FixedDecimal::from_str("7.57").unwrap();
dec.half_even_to_increment(-1, RoundingIncrement::MultiplesOf5);
assert_eq!("7.5", dec.to_string());
let mut dec = FixedDecimal::from_str("5.45").unwrap();
dec.half_even_to_increment(-2, RoundingIncrement::MultiplesOf25);
assert_eq!("5.50", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_even_to_increment(-1, RoundingIncrement::MultiplesOf25);
assert_eq!("10.0", dec.to_string());
let mut dec = FixedDecimal::from_str("9.99").unwrap();
dec.half_even_to_increment(-2, RoundingIncrement::MultiplesOf2);
assert_eq!("10.00", dec.to_string());
fn half_even_to_increment_internal<R: IncrementLike>( &mut self, position: i16, increment: R, )
sourcepub fn half_evened(self, position: i16) -> Self
pub fn half_evened(self, position: i16) -> Self
Take the half even of the number at a particular position.
§Examples
use fixed_decimal::FixedDecimal;
let dec = FixedDecimal::from_str("-1.5").unwrap();
assert_eq!("-2", dec.half_evened(0).to_string());
let dec = FixedDecimal::from_str("0.4").unwrap();
assert_eq!("0", dec.half_evened(0).to_string());
let dec = FixedDecimal::from_str("0.5").unwrap();
assert_eq!("0", dec.half_evened(0).to_string());
let dec = FixedDecimal::from_str("0.6").unwrap();
assert_eq!("1", dec.half_evened(0).to_string());
let dec = FixedDecimal::from_str("1.5").unwrap();
assert_eq!("2", dec.half_evened(0).to_string());
sourcepub fn half_evened_to_increment(
self,
position: i16,
increment: RoundingIncrement,
) -> Self
pub fn half_evened_to_increment( self, position: i16, increment: RoundingIncrement, ) -> Self
Take the half even of the number at a particular position and rounding increment.
§Examples
use fixed_decimal::{FixedDecimal, RoundingIncrement};
let mut dec = FixedDecimal::from_str("-3.5").unwrap();
assert_eq!(
"-4",
dec.half_evened_to_increment(0, RoundingIncrement::MultiplesOf2)
.to_string()
);
let mut dec = FixedDecimal::from_str("7.57").unwrap();
assert_eq!(
"7.5",
dec.half_evened_to_increment(-1, RoundingIncrement::MultiplesOf5)
.to_string()
);
let mut dec = FixedDecimal::from_str("5.45").unwrap();
assert_eq!(
"5.50",
dec.half_evened_to_increment(-2, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.0",
dec.half_evened_to_increment(-1, RoundingIncrement::MultiplesOf25)
.to_string()
);
let mut dec = FixedDecimal::from_str("9.99").unwrap();
assert_eq!(
"10.00",
dec.half_evened_to_increment(-2, RoundingIncrement::MultiplesOf2)
.to_string()
);
sourcepub fn concatenated_end(self, other: FixedDecimal) -> Result<Self, FixedDecimal>
pub fn concatenated_end(self, other: FixedDecimal) -> Result<Self, FixedDecimal>
Concatenate another FixedDecimal
into the end of this FixedDecimal
.
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::FixedDecimal;
let integer = FixedDecimal::from(123);
let fraction = FixedDecimal::from(456).multiplied_pow10(-3);
let result = integer.concatenated_end(fraction).expect("nonoverlapping");
assert_eq!("123.456", result.to_string());
sourcepub fn concatenate_end(
&mut self,
other: FixedDecimal,
) -> Result<(), FixedDecimal>
pub fn concatenate_end( &mut self, other: FixedDecimal, ) -> Result<(), FixedDecimal>
Concatenate another FixedDecimal
into the end of this FixedDecimal
.
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::FixedDecimal;
let mut integer = FixedDecimal::from(123);
let fraction = FixedDecimal::from(456).multiplied_pow10(-3);
integer.concatenate_end(fraction);
assert_eq!("123.456", integer.to_string());
sourcefn append_digits(&mut self, inner_zeroes: usize, new_digits: &[u8])
fn append_digits(&mut self, inner_zeroes: usize, new_digits: &[u8])
Appends a slice of digits to the end of self.digits
with optional inner zeroes.
This function does not check invariants.
sourcefn check_invariants(&self)
fn check_invariants(&self)
Assert that the invariants among struct fields are enforced. Returns true if all are okay. Call this in any method that mutates the struct fields.
Example: debug_assert!(self.check_invariants())
source§impl FixedDecimal
impl FixedDecimal
sourcepub fn try_from_f64(
float: f64,
precision: FloatPrecision,
) -> Result<Self, Error>
pub fn try_from_f64( float: f64, precision: FloatPrecision, ) -> Result<Self, Error>
Construct a FixedDecimal
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::{FixedDecimal, FloatPrecision};
use writeable::assert_writeable_eq;
let decimal =
FixedDecimal::try_from_f64(-5.1, FloatPrecision::Magnitude(-2))
.expect("Finite quantity with limited precision");
assert_writeable_eq!(decimal, "-5.10");
let decimal =
FixedDecimal::try_from_f64(0.012345678, FloatPrecision::Floating)
.expect("Finite quantity");
assert_writeable_eq!(decimal, "0.012345678");
let decimal =
FixedDecimal::try_from_f64(12345678000., FloatPrecision::Integer)
.expect("Finite, integer-valued quantity");
assert_writeable_eq!(decimal, "12345678000");
Negative zero is supported.
use fixed_decimal::{FixedDecimal, 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 =
FixedDecimal::try_from_f64(-0.0, FloatPrecision::Integer)
.expect("Negative zero");
assert_writeable_eq!(negative_zero, "-0");
sourcefn new_from_f64_raw(float: f64) -> Result<Self, Error>
fn new_from_f64_raw(float: f64) -> Result<Self, Error>
Internal function for parsing directly from floats using ryū
Trait Implementations§
source§impl Clone for FixedDecimal
impl Clone for FixedDecimal
source§fn clone(&self) -> FixedDecimal
fn clone(&self) -> FixedDecimal
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for FixedDecimal
impl Debug for FixedDecimal
source§impl Default for FixedDecimal
impl Default for FixedDecimal
source§impl Display for FixedDecimal
impl Display for FixedDecimal
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 FixedDecimal
impl From<FixedInteger> for FixedDecimal
source§fn from(value: FixedInteger) -> Self
fn from(value: FixedInteger) -> Self
source§impl From<i128> for FixedDecimal
impl From<i128> for FixedDecimal
source§impl From<i16> for FixedDecimal
impl From<i16> for FixedDecimal
source§impl From<i32> for FixedDecimal
impl From<i32> for FixedDecimal
source§impl From<i64> for FixedDecimal
impl From<i64> for FixedDecimal
source§impl From<i8> for FixedDecimal
impl From<i8> for FixedDecimal
source§impl From<isize> for FixedDecimal
impl From<isize> for FixedDecimal
source§impl From<u128> for FixedDecimal
impl From<u128> for FixedDecimal
source§impl From<u16> for FixedDecimal
impl From<u16> for FixedDecimal
source§impl From<u32> for FixedDecimal
impl From<u32> for FixedDecimal
source§impl From<u64> for FixedDecimal
impl From<u64> for FixedDecimal
source§impl From<u8> for FixedDecimal
impl From<u8> for FixedDecimal
source§impl From<usize> for FixedDecimal
impl From<usize> for FixedDecimal
source§impl FromStr for FixedDecimal
impl FromStr for FixedDecimal
source§impl PartialEq for FixedDecimal
impl PartialEq for FixedDecimal
source§impl TryFrom<&[u8]> for FixedDecimal
impl TryFrom<&[u8]> for FixedDecimal
source§impl TryFrom<FixedDecimal> for FixedInteger
impl TryFrom<FixedDecimal> for FixedInteger
source§type Error = FixedDecimalError
type Error = FixedDecimalError
source§impl Writeable for FixedDecimal
impl Writeable for FixedDecimal
Render the FixedDecimal
as a string of ASCII digits with a possible decimal point.
§Examples
assert_writeable_eq!(FixedDecimal::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.impl StructuralPartialEq for FixedDecimal
Auto Trait Implementations§
impl Freeze for FixedDecimal
impl RefUnwindSafe for FixedDecimal
impl Send for FixedDecimal
impl Sync for FixedDecimal
impl Unpin for FixedDecimal
impl UnwindSafe for FixedDecimal
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)