Struct rust_decimal::Decimal[][src]

pub struct Decimal { /* fields omitted */ }

Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 <= m <= 296, and e is an integer between 0 and 28 inclusive.

Methods

impl Decimal
[src]

Returns a Decimal with a 64 bit m representation and corresponding e scale.

Arguments

  • num - An i64 that represents the m portion of the decimal number
  • scale - A u32 representing the e portion of the decimal number.

Example

use rust_decimal::Decimal;

let pi = Decimal::new(3141, 3);
assert_eq!(pi.to_string(), "3.141");

Returns a Decimal using the instances constituent parts.

Arguments

  • lo - The low 32 bits of a 96-bit integer.
  • mid - The middle 32 bits of a 96-bit integer.
  • hi - The high 32 bits of a 96-bit integer.
  • negative - true to indicate a negative number.
  • scale - A power of 10 ranging from 0 to 28.

Example

use rust_decimal::Decimal;

let pi = Decimal::from_parts(1102470952, 185874565, 1703060790, false, 28);
assert_eq!(pi.to_string(), "3.1415926535897932384626433832");

Returns a Result which if successful contains the Decimal constitution of the scientific notation provided by value.

Arguments

  • value - The scientific notation of the Decimal.

Example

use rust_decimal::Decimal;

let value = Decimal::from_scientific("9.7e-7").unwrap();
assert_eq!(value.to_string(), "0.00000097");

Returns the scale of the decimal number, otherwise known as e.

Example

use rust_decimal::Decimal;

let num = Decimal::new(1234, 3);
assert_eq!(num.scale(), 3u32);

An optimized method for changing the sign of a decimal number.

Arguments

  • positive: true if the resulting decimal should be positive.

Example

use rust_decimal::Decimal;

let mut one = Decimal::new(1, 0);
one.set_sign(false);
assert_eq!(one.to_string(), "-1");

An optimized method for changing the scale of a decimal number.

Arguments

  • scale: the new scale of the number

Example

use rust_decimal::Decimal;

let mut one = Decimal::new(1, 0);
one.set_scale(5);
assert_eq!(one.to_string(), "0.00001");

Returns a serialized version of the decimal number. The resulting byte array will have the following representation:

  • Bytes 1-4: flags
  • Bytes 5-8: lo portion of m
  • Bytes 9-12: mid portion of m
  • Bytes 13-16: high portion of m

Deserializes the given bytes into a decimal number. The deserialized byte representation must be 16 bytes and adhere to the followign convention:

  • Bytes 1-4: flags
  • Bytes 5-8: lo portion of m
  • Bytes 9-12: mid portion of m
  • Bytes 13-16: high portion of m

Deprecated since 0.6.3

: please use is_sign_negative instead

Returns true if the decimal is negative.

Deprecated since 0.6.3

: please use is_sign_positive instead

Returns true if the decimal is positive.

Returns true if the decimal is negative.

Returns true if the decimal is positive.

Returns the minimum possible number that Decimal can represent.

Returns the maximum possible number that Decimal can represent.

Returns a new Decimal integral with no fractional portion. This is a true truncation whereby no rounding is performed.

Example

use rust_decimal::Decimal;

let pi = Decimal::new(3141, 3);
let trunc = Decimal::new(3, 0);
// note that it returns a decimal
assert_eq!(pi.trunc(), trunc);

Returns a new Decimal representing the fractional portion of the number.

Example

use rust_decimal::Decimal;

let pi = Decimal::new(3141, 3);
let fract = Decimal::new(141, 3);
// note that it returns a decimal
assert_eq!(pi.fract(), fract);

Computes the absolute value of self.

Example

use rust_decimal::Decimal;

let num = Decimal::new(-3141, 3);
assert_eq!(num.abs().to_string(), "3.141");

Returns the largest integer less than or equal to a number.

Example

use rust_decimal::Decimal;

let num = Decimal::new(3641, 3);
assert_eq!(num.floor().to_string(), "3");

Returns the smallest integer greater than or equal to a number.

Example

use rust_decimal::Decimal;

let num = Decimal::new(3141, 3);
assert_eq!(num.ceil().to_string(), "4");
let num = Decimal::new(3, 0);
assert_eq!(num.ceil().to_string(), "3");

Strips any trailing zero's from a Decimal.

Example

use rust_decimal::Decimal;

let number = Decimal::new(3100, 3);
// note that it returns a decimal, without the extra scale
assert_eq!(number.normalize().to_string(), "3.1");

Returns a new Decimal number with no fractional portion (i.e. an integer). Rounding currently follows "Bankers Rounding" rules. e.g. 6.5 -> 6, 7.5 -> 8

Example

use rust_decimal::Decimal;

// Demonstrating bankers rounding...
let number_down = Decimal::new(65, 1);
let number_up   = Decimal::new(75, 1);
assert_eq!(number_down.round().to_string(), "6");
assert_eq!(number_up.round().to_string(), "8");

Returns a new Decimal number with the specified number of decimal points for fractional portion. Rounding is performed using the provided RoundingStrategy

Arguments

  • dp: the number of decimal points to round to.

Example

use rust_decimal::{Decimal, RoundingStrategy};
use std::str::FromStr;

let tax = Decimal::from_str("3.4395").unwrap();
assert_eq!(tax.round_dp_with_strategy(2, RoundingStrategy::RoundHalfUp).to_string(), "3.44");

Returns a new Decimal number with the specified number of decimal points for fractional portion. Rounding currently follows "Bankers Rounding" rules. e.g. 6.5 -> 6, 7.5 -> 8

Arguments

  • dp: the number of decimal points to round to.

Example

use rust_decimal::Decimal;
use std::str::FromStr;

let pi = Decimal::from_str("3.1415926535897932384626433832").unwrap();
assert_eq!(pi.round_dp(2).to_string(), "3.14");

Convert Decimal to unpacked representation UnpackedDecimal

Trait Implementations

impl Clone for Decimal
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Copy for Decimal
[src]

impl From<isize> for Decimal
[src]

Performs the conversion.

impl From<i8> for Decimal
[src]

Performs the conversion.

impl From<i16> for Decimal
[src]

Performs the conversion.

impl From<i32> for Decimal
[src]

Performs the conversion.

impl From<i64> for Decimal
[src]

Performs the conversion.

impl From<usize> for Decimal
[src]

Performs the conversion.

impl From<u8> for Decimal
[src]

Performs the conversion.

impl From<u16> for Decimal
[src]

Performs the conversion.

impl From<u32> for Decimal
[src]

Performs the conversion.

impl From<u64> for Decimal
[src]

Performs the conversion.

impl Zero for Decimal
[src]

Returns true if self is equal to the additive identity.

Returns the additive identity element of Self, 0. Read more

impl One for Decimal
[src]

Returns the multiplicative identity element of Self, 1. Read more

Returns true if self is equal to the multiplicative identity. Read more

impl FromStr for Decimal
[src]

The associated error which can be returned from parsing.

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

impl FromPrimitive for Decimal
[src]

Convert an i32 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an i64 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an u32 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an u64 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert a f32 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert a f64 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an isize to return an optional value of this type. If the value cannot be represented by this value, then None is returned. Read more

Convert an i8 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an i16 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an i128 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert a usize to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an u8 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an u16 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

Convert an u128 to return an optional value of this type. If the type cannot be represented by this value, then None is returned. Read more

impl ToPrimitive for Decimal
[src]

Converts the value of self to an f64.

Converts the value of self to an i64.

Converts the value of self to an u64.

Converts the value of self to an isize.

Converts the value of self to an i8.

Converts the value of self to an i16.

Converts the value of self to an i32.

Converts the value of self to an i128. Read more

Converts the value of self to a usize.

Converts the value of self to an u8.

Converts the value of self to an u16.

Converts the value of self to an u32.

Converts the value of self to an u128. Read more

Converts the value of self to an f32.

impl Display for Decimal
[src]

Formats the value using the given formatter. Read more

impl Debug for Decimal
[src]

Formats the value using the given formatter. Read more

impl Neg for Decimal
[src]

The resulting type after applying the - operator.

Performs the unary - operation.

impl<'a> Neg for &'a Decimal
[src]

The resulting type after applying the - operator.

Performs the unary - operation.

impl Add<Decimal> for Decimal
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<'a> Add<Decimal> for &'a Decimal
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<'a> Add<&'a Decimal> for Decimal
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl<'a, 'b> Add<&'b Decimal> for &'a Decimal
[src]

The resulting type after applying the + operator.

Performs the + operation.

impl AddAssign for Decimal
[src]

Performs the += operation.

impl Sub<Decimal> for Decimal
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl<'a> Sub<Decimal> for &'a Decimal
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl<'a> Sub<&'a Decimal> for Decimal
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl<'a, 'b> Sub<&'b Decimal> for &'a Decimal
[src]

The resulting type after applying the - operator.

Performs the - operation.

impl SubAssign for Decimal
[src]

Performs the -= operation.

impl Mul<Decimal> for Decimal
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl<'a> Mul<Decimal> for &'a Decimal
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl<'a> Mul<&'a Decimal> for Decimal
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl<'a, 'b> Mul<&'b Decimal> for &'a Decimal
[src]

The resulting type after applying the * operator.

Performs the * operation.

impl MulAssign for Decimal
[src]

Performs the *= operation.

impl Div<Decimal> for Decimal
[src]

The resulting type after applying the / operator.

Performs the / operation.

impl<'a> Div<Decimal> for &'a Decimal
[src]

The resulting type after applying the / operator.

Performs the / operation.

impl<'a> Div<&'a Decimal> for Decimal
[src]

The resulting type after applying the / operator.

Performs the / operation.

impl<'a, 'b> Div<&'b Decimal> for &'a Decimal
[src]

The resulting type after applying the / operator.

Performs the / operation.

impl DivAssign for Decimal
[src]

Performs the /= operation.

impl Rem<Decimal> for Decimal
[src]

The resulting type after applying the % operator.

Performs the % operation.

impl<'a> Rem<Decimal> for &'a Decimal
[src]

The resulting type after applying the % operator.

Performs the % operation.

impl<'a> Rem<&'a Decimal> for Decimal
[src]

The resulting type after applying the % operator.

Performs the % operation.

impl<'a, 'b> Rem<&'b Decimal> for &'a Decimal
[src]

The resulting type after applying the % operator.

Performs the % operation.

impl RemAssign for Decimal
[src]

Performs the %= operation.

impl PartialEq for Decimal
[src]

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

This method tests for !=.

impl Eq for Decimal
[src]

impl Hash for Decimal
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl PartialOrd for Decimal
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Ord for Decimal
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl<'de> Deserialize<'de> for Decimal
[src]

Deserialize this value from the given Serde deserializer. Read more

impl Serialize for Decimal
[src]

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

impl Send for Decimal

impl Sync for Decimal