pub struct Amount { /* private fields */ }
Expand description
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");
Implementations§
Trait Implementations§
Source§impl AddAssign for Amount
impl AddAssign for Amount
Source§fn add_assign(&mut self, rhs: Amount)
fn add_assign(&mut self, rhs: Amount)
+=
operation. Read moreSource§impl DivAssign<u16> for Amount
impl DivAssign<u16> for Amount
Source§fn div_assign(&mut self, rhs: u16)
fn div_assign(&mut self, rhs: u16)
/=
operation. Read moreSource§impl DivAssign<u32> for Amount
impl DivAssign<u32> for Amount
Source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
/=
operation. Read moreSource§impl DivAssign<u64> for Amount
impl DivAssign<u64> for Amount
Source§fn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
/=
operation. Read moreSource§impl DivAssign<u8> for Amount
impl DivAssign<u8> for Amount
Source§fn div_assign(&mut self, rhs: u8)
fn div_assign(&mut self, rhs: u8)
/=
operation. Read moreSource§impl DivAssign<usize> for Amount
impl DivAssign<usize> for Amount
Source§fn div_assign(&mut self, rhs: usize)
fn div_assign(&mut self, rhs: usize)
/=
operation. Read moreSource§impl MulAssign<u16> for Amount
impl MulAssign<u16> for Amount
Source§fn mul_assign(&mut self, rhs: u16)
fn mul_assign(&mut self, rhs: u16)
*=
operation. Read moreSource§impl MulAssign<u32> for Amount
impl MulAssign<u32> for Amount
Source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moreSource§impl MulAssign<u64> for Amount
impl MulAssign<u64> for Amount
Source§fn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
*=
operation. Read moreSource§impl MulAssign<u8> for Amount
impl MulAssign<u8> for Amount
Source§fn mul_assign(&mut self, rhs: u8)
fn mul_assign(&mut self, rhs: u8)
*=
operation. Read moreSource§impl MulAssign<usize> for Amount
impl MulAssign<usize> for Amount
Source§fn mul_assign(&mut self, rhs: usize)
fn mul_assign(&mut self, rhs: usize)
*=
operation. Read moreSource§impl Ord for Amount
impl Ord for Amount
Source§impl PartialOrd for Amount
impl PartialOrd for Amount
Source§impl RemAssign<u16> for Amount
impl RemAssign<u16> for Amount
Source§fn rem_assign(&mut self, rhs: u16)
fn rem_assign(&mut self, rhs: u16)
%=
operation. Read moreSource§impl RemAssign<u32> for Amount
impl RemAssign<u32> for Amount
Source§fn rem_assign(&mut self, rhs: u32)
fn rem_assign(&mut self, rhs: u32)
%=
operation. Read moreSource§impl RemAssign<u64> for Amount
impl RemAssign<u64> for Amount
Source§fn rem_assign(&mut self, rhs: u64)
fn rem_assign(&mut self, rhs: u64)
%=
operation. Read moreSource§impl RemAssign<u8> for Amount
impl RemAssign<u8> for Amount
Source§fn rem_assign(&mut self, rhs: u8)
fn rem_assign(&mut self, rhs: u8)
%=
operation. Read moreSource§impl RemAssign<usize> for Amount
impl RemAssign<usize> for Amount
Source§fn rem_assign(&mut self, rhs: usize)
fn rem_assign(&mut self, rhs: usize)
%=
operation. Read moreSource§impl SubAssign for Amount
impl SubAssign for Amount
Source§fn sub_assign(&mut self, rhs: Amount)
fn sub_assign(&mut self, rhs: Amount)
-=
operation. Read moreimpl Copy for Amount
impl Eq for Amount
impl StructuralPartialEq for Amount
Auto Trait Implementations§
impl Freeze for Amount
impl RefUnwindSafe for Amount
impl Send for Amount
impl Sync for Amount
impl Unpin for Amount
impl UnwindSafe for Amount
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)