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

pub struct FixedDecimal { /* fields omitted */ }
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, 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

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

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

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

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

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

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns a FixedDecimal representing zero.

Renders the FixedDecimal according to the syntax documented in FixedDecimal::write_to.

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

Creates a new String with the data from this Writeable. Like ToString, but smaller and faster. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.