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]
fn from_repr(value: u64) -> Amount
[src]
Creates a new amount from its internal representation.
fn get_repr(&self) -> u64
[src]
Gets the internal representation of the amount.
fn min_value() -> Amount
[src]
Returns the smallest value that can be represented as a currency amount.
fn max_value() -> 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]
fn clone(&self) -> Amount
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl PartialEq for Amount
[src]
fn eq(&self, __arg_0: &Amount) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Amount) -> bool
[src]
This method tests for !=
.
impl Eq for Amount
[src]
impl PartialOrd for Amount
[src]
fn partial_cmp(&self, __arg_0: &Amount) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Amount) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Amount) -> bool
[src]
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
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Amount) -> bool
[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]
fn cmp(&self, __arg_0: &Amount) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more
impl Display for Amount
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
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>
[src]
Parses a string s
to return a value of this type. Read more
impl Debug for Amount
[src]
impl Encodable for Amount
[src]
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>
[src]
Serialize a value using an Encoder
.
impl Decodable for Amount
[src]
fn decode<D: Decoder>(d: &mut D) -> Result<Amount, D::Error>
[src]
Deserialize a value using a Decoder
.
impl Div<u8> for Amount
[src]
type Output = Amount
The resulting type after applying the /
operator.
fn div(self, rhs: u8) -> Amount
[src]
Performs the /
operation.
impl DivAssign<u8> for Amount
[src]
fn div_assign(&mut self, rhs: u8)
[src]
Performs the /=
operation.
impl Rem<u8> for Amount
[src]
type Output = Amount
The resulting type after applying the %
operator.
fn rem(self, rhs: u8) -> Amount
[src]
Performs the %
operation.
impl RemAssign<u8> for Amount
[src]
fn rem_assign(&mut self, rhs: u8)
[src]
Performs the %=
operation.
impl Mul<u8> for Amount
[src]
type Output = Amount
The resulting type after applying the *
operator.
fn mul(self, rhs: u8) -> Amount
[src]
Performs the *
operation.
impl MulAssign<u8> for Amount
[src]
fn mul_assign(&mut self, rhs: u8)
[src]
Performs the *=
operation.
impl Div<u16> for Amount
[src]
type Output = Amount
The resulting type after applying the /
operator.
fn div(self, rhs: u16) -> Amount
[src]
Performs the /
operation.
impl DivAssign<u16> for Amount
[src]
fn div_assign(&mut self, rhs: u16)
[src]
Performs the /=
operation.
impl Rem<u16> for Amount
[src]
type Output = Amount
The resulting type after applying the %
operator.
fn rem(self, rhs: u16) -> Amount
[src]
Performs the %
operation.
impl RemAssign<u16> for Amount
[src]
fn rem_assign(&mut self, rhs: u16)
[src]
Performs the %=
operation.
impl Mul<u16> for Amount
[src]
type Output = Amount
The resulting type after applying the *
operator.
fn mul(self, rhs: u16) -> Amount
[src]
Performs the *
operation.
impl MulAssign<u16> for Amount
[src]
fn mul_assign(&mut self, rhs: u16)
[src]
Performs the *=
operation.
impl Div<u32> for Amount
[src]
type Output = Amount
The resulting type after applying the /
operator.
fn div(self, rhs: u32) -> Amount
[src]
Performs the /
operation.
impl DivAssign<u32> for Amount
[src]
fn div_assign(&mut self, rhs: u32)
[src]
Performs the /=
operation.
impl Rem<u32> for Amount
[src]
type Output = Amount
The resulting type after applying the %
operator.
fn rem(self, rhs: u32) -> Amount
[src]
Performs the %
operation.
impl RemAssign<u32> for Amount
[src]
fn rem_assign(&mut self, rhs: u32)
[src]
Performs the %=
operation.
impl Mul<u32> for Amount
[src]
type Output = Amount
The resulting type after applying the *
operator.
fn mul(self, rhs: u32) -> Amount
[src]
Performs the *
operation.
impl MulAssign<u32> for Amount
[src]
fn mul_assign(&mut self, rhs: u32)
[src]
Performs the *=
operation.
impl Div<u64> for Amount
[src]
type Output = Amount
The resulting type after applying the /
operator.
fn div(self, rhs: u64) -> Amount
[src]
Performs the /
operation.
impl DivAssign<u64> for Amount
[src]
fn div_assign(&mut self, rhs: u64)
[src]
Performs the /=
operation.
impl Rem<u64> for Amount
[src]
type Output = Amount
The resulting type after applying the %
operator.
fn rem(self, rhs: u64) -> Amount
[src]
Performs the %
operation.
impl RemAssign<u64> for Amount
[src]
fn rem_assign(&mut self, rhs: u64)
[src]
Performs the %=
operation.
impl Mul<u64> for Amount
[src]
type Output = Amount
The resulting type after applying the *
operator.
fn mul(self, rhs: u64) -> Amount
[src]
Performs the *
operation.
impl MulAssign<u64> for Amount
[src]
fn mul_assign(&mut self, rhs: u64)
[src]
Performs the *=
operation.
impl Div<usize> for Amount
[src]
type Output = Amount
The resulting type after applying the /
operator.
fn div(self, rhs: usize) -> Amount
[src]
Performs the /
operation.
impl DivAssign<usize> for Amount
[src]
fn div_assign(&mut self, rhs: usize)
[src]
Performs the /=
operation.
impl Rem<usize> for Amount
[src]
type Output = Amount
The resulting type after applying the %
operator.
fn rem(self, rhs: usize) -> Amount
[src]
Performs the %
operation.
impl RemAssign<usize> for Amount
[src]
fn rem_assign(&mut self, rhs: usize)
[src]
Performs the %=
operation.
impl Mul<usize> for Amount
[src]
type Output = Amount
The resulting type after applying the *
operator.
fn mul(self, rhs: usize) -> Amount
[src]
Performs the *
operation.
impl MulAssign<usize> for Amount
[src]
fn mul_assign(&mut self, rhs: usize)
[src]
Performs the *=
operation.
impl Add for Amount
[src]
type Output = Amount
The resulting type after applying the +
operator.
fn add(self, rhs: Amount) -> Amount
[src]
Performs the +
operation.
impl AddAssign for Amount
[src]
fn add_assign(&mut self, rhs: Amount)
[src]
Performs the +=
operation.
impl Sub for Amount
[src]
type Output = Amount
The resulting type after applying the -
operator.
fn sub(self, rhs: Amount) -> Amount
[src]
Performs the -
operation.
impl SubAssign for Amount
[src]
fn sub_assign(&mut self, rhs: Amount)
[src]
Performs the -=
operation.