currency

Holds an amount of currency *

Constructors

this
this(double x)

Floating point contructor. Uses rmode on x.

this
this(Range s)

String contructor.

Members

Aliases

T
alias T = typeof(this)
Undocumented in source.

Functions

opBinary
T opBinary(T rhs)

Can add and subtract money amounts of the same type.

opBinary
double opBinary(T rhs)

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

opBinary
T opBinary(long rhs)

Can multiply, divide, and modulo with integer values.

opBinary
T opBinary(real rhs)

Can multiply, divide, and modulo floating point numbers.

opBinaryRight
T opBinaryRight(real lhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
T opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
T opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
int opCmp(OT other)

Can compare with money amounts of the same concurrency.

opCmp
int opCmp(T other)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(OT other)

Can check equality with money amounts of the same concurrency and decimal places.

opEquals
bool opEquals(T other)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
void opOpAssign(T rhs)

Can add and subtract money amounts of the same type.

opOpAssign
void opOpAssign(long rhs)

Can multiply, divide, and modulo with integer values.

opOpAssign
void opOpAssign(real rhs)

Can multiply, divide, and modulo floating point numbers.

opUnary
T opUnary()
Undocumented in source. Be warned that the author may not have intended to support it.
opUnary
T opUnary()
Undocumented in source. Be warned that the author may not have intended to support it.
toDecimalString
void toDecimalString(void delegate(const(char)[]) sink, FormatSpec!char fmt)
Undocumented in source. Be warned that the author may not have intended to support it.
toString
void toString(void delegate(const(char)[]) sink, FormatSpec!char fmt)

Can convert to string.

Manifest constants

__currency
enum __currency;
Undocumented in source.
__dec_places
enum __dec_places;
Undocumented in source.
__factor
enum __factor;
Undocumented in source.
__rmode
enum __rmode;
Undocumented in source.

Static variables

init
auto init;

default initialisation value is zero

max
auto max;

maximum amount depends on dec_places

min
auto min;

minimum amount depends on dec_places

Variables

amount
long amount;
Undocumented in source.

Examples

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)));

Meta