Function coins
Make an Amount with the number of coins
Parameters
Name | Description |
---|---|
num_coins | the number of coins |
Returns
The Amount calculated with cents per unit and the number of coins
Example
// Typical use case is to do `if (!op) error_handling;`
Amount two = Amount .UnitPerCoin;
assert(two .add(two));
assert(two .sub(two));
// The value is still valid
assert(two .isValid());
// This should error
assert(!two .sub(Amount(1)));
// The value was poisoned
assert(!two .isValid());
// Even substracting it to itself (which should yield 0) doesn't work
assert(!two .sub(two));
// But can be reset to a sane value if needed
two = Amount(1);
assert(two .sub(Amount(1)));
// Tests for division with remainder
Amount val = Amount .MinFreezeAmount;
assert(val .div(3) == Amount(1));
assert(val .integral() == 13_333);
assert(val .decimal() == 3_333_333);
assert(val .div(100) == Amount(33));
assert(val .integral() == 133);
assert(val .decimal() == 3_333_333);
// Division without remainder
val = Amount .MinFreezeAmount;
assert(val .div(1000) == Amount(0));
assert(val .integral() == 40);
assert(val .decimal() == 0);
// Division edge cases
val = Amount .UnitPerCoin;
assert(val .div(0) == Amount(0));
assert(val == Amount .UnitPerCoin);
assert(val .div(0) == Amount(0));
assert(val == Amount .UnitPerCoin);
Example
comparisons
assert(Amount(100) > Amount(99));
assert(Amount(99) < Amount(100));
assert(Amount(100) >= Amount(100));
assert(!(Amount(100) > Amount(100)));
const am1 = Amount(99);
const am2 = Amount(100);
assert(am1 < am2); // const
import std .algorithm; // array
auto arr = [Amount(300), Amount(100), Amount(100), Amount(200)];
arr .sort!((a, b) => a > b);
assert(arr == [Amount(300), Amount(200), Amount(100), Amount(100)]);
arr .sort!((a, b) => a < b);
assert(arr == [Amount(100), Amount(100), Amount(200), Amount(300)]);