[−][src]Struct fixed_decimal::decimal::FixedDecimal
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, used for formatting and plural selection.
You can create a FixedDecimal
from a standard integer type. To represent fraction digits,
call .multiply_pow10()
after creating your FixedDecimal
.
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
is_negative: bool
Whether the number is negative. Negative zero is supported.
Example
use fixed_decimal::FixedDecimal; let mut dec: FixedDecimal = Default::default(); dec.is_negative = true; assert_eq!("-0", dec.to_string());
Implementations
impl FixedDecimal
[src]
pub fn digit_at(&self, magnitude: i16) -> u8
[src]
Gets the digit at the specified order of magnitude. Returns 0 if the magnitude is out of range of the currently visible digits.
Example
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));
pub const fn magnitude_range(&self) -> RangeInclusive<i16>
[src]
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.
Example
use fixed_decimal::FixedDecimal; let mut dec = FixedDecimal::from(120); assert_eq!(0..=2, dec.magnitude_range());
pub fn multiply_pow10(&mut self, delta: i16) -> Result<(), Error>
[src]
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.
Can fail if the change in magnitude pushes the digits out of bounds; the magnitudes of all digits should fit in an i16.
Example
use fixed_decimal::FixedDecimal; let mut dec = FixedDecimal::from(42); assert_eq!("42", dec.to_string()); dec.multiply_pow10(3).expect("Bounds are small"); assert_eq!("42000", dec.to_string());
pub fn multiplied_pow10(self, delta: i16) -> Result<Self, Error>
[src]
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.
Can fail if the change in magnitude pushes the digits out of bounds; the magnitudes of all digits should fit in an i16.
Example
use fixed_decimal::FixedDecimal; let dec = FixedDecimal::from(42).multiplied_pow10(3).expect("Bounds are small"); assert_eq!("42000", dec.to_string());
pub fn write_to(&self, sink: &mut dyn Write) -> Result
[src]
Render the FixedDecimal
as a string of ASCII digits with a possible decimal point.
Example
use fixed_decimal::FixedDecimal; let dec = FixedDecimal::from(42); let mut result = String::with_capacity(dec.write_len()); dec.write_to(&mut result).expect("write_to(String) should not fail"); assert_eq!("42", result);
pub const fn write_len(&self) -> usize
[src]
The number of bytes that will be written by FixedDecimal::write_to
. Use this function to
pre-allocate capacity in the destination buffer.
Example
use fixed_decimal::FixedDecimal; let dec = FixedDecimal::from(-5000).multiplied_pow10(-2).expect("Bounds are small"); let mut result = String::with_capacity(dec.write_len()); dec.write_to(&mut result).expect("write_to(String) should not fail"); assert_eq!("-50.00", result); assert_eq!(6, dec.write_len());
Trait Implementations
impl Clone for FixedDecimal
[src]
fn clone(&self) -> FixedDecimal
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for FixedDecimal
[src]
impl Default for FixedDecimal
[src]
impl Display for FixedDecimal
[src]
Renders the FixedDecimal
according to the syntax documented in FixedDecimal::write_to
.
impl From<i128> for FixedDecimal
[src]
impl From<i16> for FixedDecimal
[src]
impl From<i32> for FixedDecimal
[src]
impl From<i64> for FixedDecimal
[src]
impl From<i8> for FixedDecimal
[src]
impl From<isize> for FixedDecimal
[src]
impl From<u128> for FixedDecimal
[src]
impl From<u16> for FixedDecimal
[src]
impl From<u32> for FixedDecimal
[src]
impl From<u64> for FixedDecimal
[src]
impl From<u8> for FixedDecimal
[src]
impl From<usize> for FixedDecimal
[src]
impl FromStr for FixedDecimal
[src]
type Err = Error
The associated error which can be returned from parsing.
fn from_str(input_str: &str) -> Result<Self, Self::Err>
[src]
impl PartialEq<FixedDecimal> for FixedDecimal
[src]
fn eq(&self, other: &FixedDecimal) -> bool
[src]
fn ne(&self, other: &FixedDecimal) -> bool
[src]
impl StructuralPartialEq for FixedDecimal
[src]
Auto Trait Implementations
impl RefUnwindSafe for FixedDecimal
impl Send for FixedDecimal
impl Sync for FixedDecimal
impl Unpin for FixedDecimal
impl UnwindSafe for FixedDecimal
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,