Struct fractal_utils::amount::Amount
[−]
[src]
pub struct Amount {
// some 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]
fn from_repr(value: u64) -> Amount
Creates a new amount from its internal representation.
fn get_repr(&self) -> u64
Gets the internal representation of the amount.
fn min_value() -> Amount
Returns the smallest value that can be represented as a currency amount.
fn max_value() -> Amount
Returns the largest value that can be represented as a currency amount.
Trait Implementations
impl Ord for Amount[src]
fn cmp(&self, __arg_0: &Amount) -> Ordering
This method returns an Ordering between self and other. Read more
impl PartialOrd for Amount[src]
fn partial_cmp(&self, __arg_0: &Amount) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, __arg_0: &Amount) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, __arg_0: &Amount) -> bool
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, __arg_0: &Amount) -> bool
This method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, __arg_0: &Amount) -> bool
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl Eq for Amount[src]
impl PartialEq for Amount[src]
fn eq(&self, __arg_0: &Amount) -> bool
This method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, __arg_0: &Amount) -> bool
This method tests for !=.
impl Clone for Amount[src]
fn clone(&self) -> Amount
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0
Performs copy-assignment from source. Read more
impl Copy for Amount[src]
impl Display for Amount[src]
impl FromStr for Amount[src]
type Err = AmountParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Amount, AmountParseError>
Parses a string s to return a value of this type. Read more
impl Debug for Amount[src]
impl Encodable for Amount[src]
impl Decodable for Amount[src]
impl Div<u8> for Amount[src]
type Output = Amount
The resulting type after applying the / operator
fn div(self, rhs: u8) -> Amount
The method for the / operator
impl DivAssign<u8> for Amount[src]
fn div_assign(&mut self, rhs: u8)
The method for the /= operator
impl Rem<u8> for Amount[src]
type Output = Amount
The resulting type after applying the % operator
fn rem(self, rhs: u8) -> Amount
The method for the % operator
impl RemAssign<u8> for Amount[src]
fn rem_assign(&mut self, rhs: u8)
The method for the %= operator
impl Mul<u8> for Amount[src]
type Output = Amount
The resulting type after applying the * operator
fn mul(self, rhs: u8) -> Amount
The method for the * operator
impl MulAssign<u8> for Amount[src]
fn mul_assign(&mut self, rhs: u8)
The method for the *= operator
impl Div<u16> for Amount[src]
type Output = Amount
The resulting type after applying the / operator
fn div(self, rhs: u16) -> Amount
The method for the / operator
impl DivAssign<u16> for Amount[src]
fn div_assign(&mut self, rhs: u16)
The method for the /= operator
impl Rem<u16> for Amount[src]
type Output = Amount
The resulting type after applying the % operator
fn rem(self, rhs: u16) -> Amount
The method for the % operator
impl RemAssign<u16> for Amount[src]
fn rem_assign(&mut self, rhs: u16)
The method for the %= operator
impl Mul<u16> for Amount[src]
type Output = Amount
The resulting type after applying the * operator
fn mul(self, rhs: u16) -> Amount
The method for the * operator
impl MulAssign<u16> for Amount[src]
fn mul_assign(&mut self, rhs: u16)
The method for the *= operator
impl Div<u32> for Amount[src]
type Output = Amount
The resulting type after applying the / operator
fn div(self, rhs: u32) -> Amount
The method for the / operator
impl DivAssign<u32> for Amount[src]
fn div_assign(&mut self, rhs: u32)
The method for the /= operator
impl Rem<u32> for Amount[src]
type Output = Amount
The resulting type after applying the % operator
fn rem(self, rhs: u32) -> Amount
The method for the % operator
impl RemAssign<u32> for Amount[src]
fn rem_assign(&mut self, rhs: u32)
The method for the %= operator
impl Mul<u32> for Amount[src]
type Output = Amount
The resulting type after applying the * operator
fn mul(self, rhs: u32) -> Amount
The method for the * operator
impl MulAssign<u32> for Amount[src]
fn mul_assign(&mut self, rhs: u32)
The method for the *= operator
impl Div<u64> for Amount[src]
type Output = Amount
The resulting type after applying the / operator
fn div(self, rhs: u64) -> Amount
The method for the / operator
impl DivAssign<u64> for Amount[src]
fn div_assign(&mut self, rhs: u64)
The method for the /= operator
impl Rem<u64> for Amount[src]
type Output = Amount
The resulting type after applying the % operator
fn rem(self, rhs: u64) -> Amount
The method for the % operator
impl RemAssign<u64> for Amount[src]
fn rem_assign(&mut self, rhs: u64)
The method for the %= operator
impl Mul<u64> for Amount[src]
type Output = Amount
The resulting type after applying the * operator
fn mul(self, rhs: u64) -> Amount
The method for the * operator
impl MulAssign<u64> for Amount[src]
fn mul_assign(&mut self, rhs: u64)
The method for the *= operator
impl Div<usize> for Amount[src]
type Output = Amount
The resulting type after applying the / operator
fn div(self, rhs: usize) -> Amount
The method for the / operator
impl DivAssign<usize> for Amount[src]
fn div_assign(&mut self, rhs: usize)
The method for the /= operator
impl Rem<usize> for Amount[src]
type Output = Amount
The resulting type after applying the % operator
fn rem(self, rhs: usize) -> Amount
The method for the % operator
impl RemAssign<usize> for Amount[src]
fn rem_assign(&mut self, rhs: usize)
The method for the %= operator
impl Mul<usize> for Amount[src]
type Output = Amount
The resulting type after applying the * operator
fn mul(self, rhs: usize) -> Amount
The method for the * operator
impl MulAssign<usize> for Amount[src]
fn mul_assign(&mut self, rhs: usize)
The method for the *= operator
impl Add for Amount[src]
type Output = Amount
The resulting type after applying the + operator
fn add(self, rhs: Amount) -> Amount
The method for the + operator
impl AddAssign for Amount[src]
fn add_assign(&mut self, rhs: Amount)
The method for the += operator
impl Sub for Amount[src]
type Output = Amount
The resulting type after applying the - operator
fn sub(self, rhs: Amount) -> Amount
The method for the - operator
impl SubAssign for Amount[src]
fn sub_assign(&mut self, rhs: Amount)
The method for the -= operator