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

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.

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

Zero-pad the number on the left to a particular number of integer digits, returning the result.

Examples
use fixed_decimal::FixedDecimal;

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

;
assert_eq!("0042", dec.clone().padded_left(4).to_string());

assert_eq!("042", dec.clone().padded_left(3).to_string());

assert_eq!("42", dec.clone().padded_left(2).to_string());

assert_eq!("42", dec.clone().padded_left(1).to_string());

Zero-pad the number on the left to a particular number of integer digits.

Examples
use fixed_decimal::FixedDecimal;

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

dec.pad_left(4);
assert_eq!("0042", dec.to_string());

dec.pad_left(3);
assert_eq!("042", dec.to_string());

dec.pad_left(2);
assert_eq!("42", dec.to_string());

dec.pad_left(1);
assert_eq!("42", dec.to_string());

Truncate the number on the left to a particular magnitude, deleting digits if necessary, returning the result.

Examples
use fixed_decimal::FixedDecimal;

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

assert_eq!("4235", dec.clone().truncated_left(5).to_string());

assert_eq!("235", dec.clone().truncated_left(2).to_string());

assert_eq!("35", dec.clone().truncated_left(1).to_string());

assert_eq!("5", dec.clone().truncated_left(0).to_string());

assert_eq!("0", dec.clone().truncated_left(-1).to_string());

Truncate the number on the left to a particular magnitude, deleting digits if necessary.

Examples
use fixed_decimal::FixedDecimal;

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

dec.truncate_left(5);
assert_eq!("4235", dec.to_string());

dec.truncate_left(2);
assert_eq!("235", dec.to_string());

dec.truncate_left(1);
assert_eq!("35", dec.to_string());

dec.truncate_left(0);
assert_eq!("5", dec.to_string());

dec.truncate_left(-1);
assert_eq!("0", dec.to_string());

Zero-pad the number on the right to a particular (negative) magnitude. Will truncate trailing zeros if necessary, but will not truncate other digits, returning the result.

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_right(1).to_string());

assert_eq!("123.456", dec.clone().padded_right(2).to_string());

assert_eq!("123.4560", dec.clone().padded_right(4).to_string());

assert_eq!("123.456000", dec.clone().padded_right(6).to_string());

Zero-pad the number on the right to a particular (negative) magnitude. Will truncate trailing zeros if necessary, but will not truncate other digits.

Examples
use fixed_decimal::FixedDecimal;

let mut dec = FixedDecimal::from_str("123.456").unwrap();
assert_eq!("123.456", dec.to_string());

dec.pad_right(1);
assert_eq!("123.456", dec.to_string());

dec.pad_right(2);
assert_eq!("123.456", dec.to_string());

dec.pad_right(4);
assert_eq!("123.4560", dec.to_string());

dec.pad_right(6);
assert_eq!("123.456000", dec.to_string());

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

Construct a FixedDecimal from an f64. This uses ryu and goes through an intermediate string representation, so is not fully efficient. See icu4x#166 for more details.

This function can be made available with the "ryu" feature.

use fixed_decimal::{DoublePrecision, FixedDecimal};
use writeable::Writeable;

let decimal = FixedDecimal::new_from_f64(0.012345678, DoublePrecision::Maximum).unwrap();
assert_eq!(decimal.writeable_to_string(), "0.012345678");

let decimal = FixedDecimal::new_from_f64(-123456.78, DoublePrecision::Maximum).unwrap();
assert_eq!(decimal.writeable_to_string(), "-123456.78");

let decimal = FixedDecimal::new_from_f64(12345678000., DoublePrecision::Maximum).unwrap();
assert_eq!(decimal.writeable_to_string(), "12345678000");

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

Write bytes and 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. Read more

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)

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.