Struct fixed_decimal::decimal::FixedDecimal[][src]

pub struct FixedDecimal { /* fields omitted */ }

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());

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.

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));

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.

Examples

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.

Examples

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.

Examples

use fixed_decimal::FixedDecimal;

let dec = FixedDecimal::from(42).multiplied_pow10(3).expect("Bounds are small");
assert_eq!("42000", dec.to_string());

pub fn negate(&mut self)[src]

Change the value from negative to positive or from positive to negative, modifying self.

Examples

use fixed_decimal::FixedDecimal;

let mut dec = FixedDecimal::from(42);
assert_eq!("42", dec.to_string());

dec.negate();
assert_eq!("-42", dec.to_string());

dec.negate();
assert_eq!("42", dec.to_string());

pub fn negated(self) -> Self[src]

Change the value from negative to positive or from positive to negative, consuming self and returning a new object.

Examples

use fixed_decimal::FixedDecimal;

assert_eq!(FixedDecimal::from(-42), FixedDecimal::from(42).negated());

pub fn signum(&self) -> Signum[src]

Returns the Signum of this FixedDecimal.

Examples

use fixed_decimal::FixedDecimal;
use fixed_decimal::Signum;

assert_eq!(Signum::AboveZero, FixedDecimal::from(42).signum());
assert_eq!(Signum::PositiveZero, FixedDecimal::from(0).signum());
assert_eq!(Signum::NegativeZero, FixedDecimal::from(0).negated().signum());
assert_eq!(Signum::BelowZero, FixedDecimal::from(-42).signum());

Trait Implementations

impl Clone for FixedDecimal[src]

impl Debug for FixedDecimal[src]

impl Default for FixedDecimal[src]

fn default() -> Self[src]

Returns a FixedDecimal representing zero.

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.

impl PartialEq<FixedDecimal> for FixedDecimal[src]

impl StructuralPartialEq for FixedDecimal[src]

impl Writeable for FixedDecimal[src]

fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result[src]

Render the FixedDecimal as a string of ASCII digits with a possible decimal point.

Examples

use fixed_decimal::FixedDecimal;
use writeable::Writeable;

let dec = FixedDecimal::from(42);
let mut result = String::with_capacity(dec.write_len().capacity());
dec.write_to(&mut result).expect("write_to(String) should not fail");
assert_eq!("42", result);

fn write_len(&self) -> LengthHint[src]

The number of bytes that will be written by FixedDecimal::write_to. Use this function to pre-allocate capacity in the destination buffer.

Examples

use fixed_decimal::FixedDecimal;
use writeable::Writeable;
use writeable::LengthHint;

let dec = FixedDecimal::from(-5000).multiplied_pow10(-2).expect("Bounds are small");
let result = dec.writeable_to_string();
assert_eq!(LengthHint::Exact(6), dec.write_len());

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.