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]

Creates a new amount from its internal representation.

Gets the internal representation of the amount.

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

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

Trait Implementations

impl Copy for Amount
[src]

impl Clone for Amount
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for Amount
[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 Amount
[src]

impl PartialOrd for Amount
[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 Amount
[src]

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

impl Display for Amount
[src]

Formats the value using the given formatter. Read more

impl Debug for Amount
[src]

Formats the value using the given formatter.

impl Div<u8> for Amount
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<u8> for Amount
[src]

The method for the /= operator

impl Rem<u8> for Amount
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<u8> for Amount
[src]

The method for the %= operator

impl Mul<u8> for Amount
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<u8> for Amount
[src]

The method for the *= operator

impl Div<u16> for Amount
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<u16> for Amount
[src]

The method for the /= operator

impl Rem<u16> for Amount
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<u16> for Amount
[src]

The method for the %= operator

impl Mul<u16> for Amount
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<u16> for Amount
[src]

The method for the *= operator

impl Div<u32> for Amount
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<u32> for Amount
[src]

The method for the /= operator

impl Rem<u32> for Amount
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<u32> for Amount
[src]

The method for the %= operator

impl Mul<u32> for Amount
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<u32> for Amount
[src]

The method for the *= operator

impl Div<u64> for Amount
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<u64> for Amount
[src]

The method for the /= operator

impl Rem<u64> for Amount
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<u64> for Amount
[src]

The method for the %= operator

impl Mul<u64> for Amount
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<u64> for Amount
[src]

The method for the *= operator

impl Div<usize> for Amount
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<usize> for Amount
[src]

The method for the /= operator

impl Rem<usize> for Amount
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<usize> for Amount
[src]

The method for the %= operator

impl Mul<usize> for Amount
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<usize> for Amount
[src]

The method for the *= operator

impl Add for Amount
[src]

The resulting type after applying the + operator

The method for the + operator

impl AddAssign for Amount
[src]

The method for the += operator

impl Sub for Amount
[src]

The resulting type after applying the - operator

The method for the - operator

impl SubAssign for Amount
[src]

The method for the -= operator