Can add and subtract money amounts of the same type.
Can divide by money amount of the same type https://en.wikipedia.org/wiki/Integer#Algebraic_properties
amount is integer, which is closed under "+|-|*", but not "/" so the return type is double to simulate rational
Can multiply, divide, and modulo with integer values.
Can multiply, divide, and modulo floating point numbers.
Can compare with money amounts of the same concurrency.
Can check equality with money amounts of the same concurrency and decimal places.
Can add and subtract money amounts of the same type.
Can multiply, divide, and modulo with integer values.
Can multiply, divide, and modulo floating point numbers.
Can convert to string.
default initialisation value is zero
maximum amount depends on dec_places
minimum amount depends on dec_places
Basic usage
alias EUR = currency!("EUR"); assert(EUR(100.0001) == EUR(100.00009)); assert(EUR(3.10) + EUR(1.40) == EUR(4.50)); assert(EUR(3.10) - EUR(1.40) == EUR(1.70)); assert(EUR(10.01) * 1.1 == EUR(11.011)); import std.format : format; // for writefln("%d", EUR(3.6)); assert(format("%d", EUR(3.6)) == "4EUR"); assert(format("%d", EUR(3.1)) == "3EUR"); // for writefln("%f", EUR(3.141592)); assert(format("%f", EUR(3.141592)) == "3.1416EUR"); assert(format("%.2f", EUR(3.145)) == "3.15EUR"); // From issue #5 assert(format("%.4f", EUR(0.01234)) == "0.0123EUR"); assert(format("%F", EUR(3.141592)) == "3.1416");
Overflow is an error, since silent corruption is worse
import std.exception : assertThrown; alias EUR = currency!("EUR"); auto one = EUR(1); assertThrown!OverflowException(EUR.max + one); assertThrown!OverflowException(EUR.min - one); assert(one > 0); assert(one < 2); assert(one == 1); assert(one == 1.0); assert(!(one > 1)); assert(!(one < 1)); assert(one != 0); assert(one != 0.0); assert(one != 2); assert(one != 2.0);
Arithmetic ignores rounding mode
alias EUR = currency!("EUR", 2, roundingMode.UP); auto one = EUR(1); assert(one != one / 3);
Generic equality and order
alias USD = currency!("USD", 2); alias EURa = currency!("EUR", 2); alias EURb = currency!("EUR", 4); alias EURc = currency!("EUR", 4, roundingMode.DOWN); // cannot compile with different currencies static assert(!__traits(compiles, EURa(1) == USD(1))); // cannot compile with different dec_places static assert(!__traits(compiles, EURa(1) == EURb(1))); // can check equality if only rounding mode differs assert(EURb(1.01) == EURc(1.01)); // cannot compare with different currencies static assert(!__traits(compiles, EURa(1) < USD(1)));
Holds an amount of currency *