Struct fractal_utils::amount::Amount [] [src]

pub struct Amount { /* fields omitted */ }

Fractal Global Credits amount

This struct can be used the same way as any other number. An Amount can be added or substracted to another Amount, and it can be divided and multiplied by an integer. All operations that are defined in the Amount scope and that are exact can be used directly as usual integer / float point operations.

No negative amounts can exist, since an Amount is unsigned, sothe negation operator '-', then, has no use with an Amount.

Its internal representation is a 64 bit unsigned number, that is displayed as a fixed point, number of factor 1/1,000. This means that an internal representation of 1,000 would be an external amount of 1. The internal representation shouldn't be used except when serializing and deserializing the data, since this type is sent in JSON as its internal u64.

The use is the following:

use fractal_utils::Amount;

let amount = Amount::from_repr(1_654); // 1.654
let ten = Amount::from_repr(10_000); // 10
let add_ten = amount + ten;
assert_eq!(add_ten, Amount::from_repr(11_654)); // 11.654

They can be divided and multiplied by any other unsigned integer:

let mut amount = Amount::from_repr(7_000); // 7
amount *= 10u32;
assert_eq!(amount, Amount::from_repr(70_000)); // 70

amount = amount / 30u16;
assert_eq!(amount, Amount::from_repr(2_333)); // 2.333

amount %= 1u8;
assert_eq!(amount, Amount::from_repr(333)); // 0.333

Amounts can easily be displayed using the Display trait as any other number:

let amount = Amount::from_repr(56_000);
assert_eq!(format!("{}", amount), "56");
assert_eq!(format!("{:.2}", amount), "56.00");
assert_eq!(format!("{:.5}", amount), "56.00000");
assert_eq!(format!("{:05.1}", amount), "056.0");

// And with rounding:
let amount = Amount::from_repr(56); // 0.056
assert_eq!(format!("{:.2}", amount), "0.06");

Methods

impl Amount
[src]

[src]

Creates a new amount from its internal representation.

[src]

Gets the internal representation of the amount.

[src]

Returns the smallest value that can be represented as a currency amount.

[src]

Returns the largest value that can be represented as a currency amount.

Trait Implementations

impl Copy for Amount
[src]

impl Clone for Amount
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl PartialEq for Amount
[src]

[src]

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

[src]

This method tests for !=.

impl Eq for Amount
[src]

impl PartialOrd for Amount
[src]

[src]

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

[src]

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

[src]

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

[src]

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

[src]

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

impl Ord for Amount
[src]

[src]

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

1.22.0
[src]

Compares and returns the maximum of two values. Read more

1.22.0
[src]

Compares and returns the minimum of two values. Read more

impl Display for Amount
[src]

[src]

Formats the value using the given formatter. Read more

impl FromStr for Amount
[src]

The associated error which can be returned from parsing.

[src]

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

impl Debug for Amount
[src]

[src]

Formats the value using the given formatter.

impl Encodable for Amount
[src]

[src]

Serialize a value using an Encoder.

impl Decodable for Amount
[src]

[src]

Deserialize a value using a Decoder.

impl Div<u8> for Amount
[src]

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl DivAssign<u8> for Amount
[src]

[src]

Performs the /= operation.

impl Rem<u8> for Amount
[src]

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl RemAssign<u8> for Amount
[src]

[src]

Performs the %= operation.

impl Mul<u8> for Amount
[src]

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl MulAssign<u8> for Amount
[src]

[src]

Performs the *= operation.

impl Div<u16> for Amount
[src]

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl DivAssign<u16> for Amount
[src]

[src]

Performs the /= operation.

impl Rem<u16> for Amount
[src]

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl RemAssign<u16> for Amount
[src]

[src]

Performs the %= operation.

impl Mul<u16> for Amount
[src]

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl MulAssign<u16> for Amount
[src]

[src]

Performs the *= operation.

impl Div<u32> for Amount
[src]

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl DivAssign<u32> for Amount
[src]

[src]

Performs the /= operation.

impl Rem<u32> for Amount
[src]

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl RemAssign<u32> for Amount
[src]

[src]

Performs the %= operation.

impl Mul<u32> for Amount
[src]

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl MulAssign<u32> for Amount
[src]

[src]

Performs the *= operation.

impl Div<u64> for Amount
[src]

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl DivAssign<u64> for Amount
[src]

[src]

Performs the /= operation.

impl Rem<u64> for Amount
[src]

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl RemAssign<u64> for Amount
[src]

[src]

Performs the %= operation.

impl Mul<u64> for Amount
[src]

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl MulAssign<u64> for Amount
[src]

[src]

Performs the *= operation.

impl Div<usize> for Amount
[src]

The resulting type after applying the / operator.

[src]

Performs the / operation.

impl DivAssign<usize> for Amount
[src]

[src]

Performs the /= operation.

impl Rem<usize> for Amount
[src]

The resulting type after applying the % operator.

[src]

Performs the % operation.

impl RemAssign<usize> for Amount
[src]

[src]

Performs the %= operation.

impl Mul<usize> for Amount
[src]

The resulting type after applying the * operator.

[src]

Performs the * operation.

impl MulAssign<usize> for Amount
[src]

[src]

Performs the *= operation.

impl Add for Amount
[src]

The resulting type after applying the + operator.

[src]

Performs the + operation.

impl AddAssign for Amount
[src]

[src]

Performs the += operation.

impl Sub for Amount
[src]

The resulting type after applying the - operator.

[src]

Performs the - operation.

impl SubAssign for Amount
[src]

[src]

Performs the -= operation.