struct Decimal

A struct representing a decimal fixed-point number.

By default, the number of digits after the decimal point is 18. The decimal point position is limited by the maximum value of the size_t data type. Decimal is based on std.bigint.BigInt and have no limitation for the number of significant digits.

this(string number, size_t fixedPointPosition = defaultFixedPoint)

Constucts an object from a decimal string.

Parameters

number

Decimal string with comma/point or without it.

fixedPointPosition

Position of decimal point in new object.

this(Decimal number, size_t fixedPointPosition = defaultFixedPoint)

Constucts an object from another Decimal object. This constructor is used to transforming precision.

Parameters

number

Decimal object.

fixedPointPosition

Position of decimal point in new object.

this(BigInt number, size_t fixedPointPosition = defaultFixedPoint)

Constucts an object from a BigInt object.

Parameters

number

BigInt

fixedPointPosition

Position of decimal point in new object.

this(T)(T number, size_t fixedPointPosition = defaultFixedPoint) 
if (is(T : long))

Constucts an object from an integer number (int/uint, long/ulong, etc.).

Parameters

number

Integer number of a built-in data type.

fixedPointPosition

Position of decimal point in new object.

this(real number, size_t fixedPointPosition = defaultFixedPoint)

Constucts an object from a floating point number. (For good precision, give preference to forming a decimal number from a string.)

Parameters

number

Floating point number of a built-in data type.

fixedPointPosition

Position of decimal point in new object.

string toFullString() const

Converts to a human readable decimal string. All characters after the dot (with all zeros) will be shown according to the configured fixed point position.

void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) const

Convert the Decimal to string, passing it to the given sink.

Available format indicators: %D (or %f instead it) and %s. For %D (or %f) there are available '+', '0' and width with precision like for floating. For example, "%+08.5D".

Parameters

sink

An OutputRange for accepting possibly piecewise segments of the formatted string.

fmt

A format string specifying the output format.

T opCast(T : bool)()

Converts to a boolean value.

T opCast(T : real)()

Converts to a floating type, if possible.

T opCast(T : long)()

Converts to an integer type, if possible.

Decimal abs() pure const

Gets the modulus.

Decimal round(size_t lastDigitPosition = 0) pure const

Calculates and returns the rounded value. The fixed point position in the return value remains the original.

Parameters

lastDigitPosition

The number of decimal places to save. By default, 0.

int getSign() pure const

Gets a sign of the decimal number (-1, 0 or 1).

bool opEquals(Decimal rhs)

Implements Decimal equality test with other Decimal.

bool opEquals(T)(T rhs)

Implements Decimal equality test with buil-in numeric types and strings.

int opCmp(Decimal rhs)

Implements comparison of Decimal with other Decimal.

int opCmp(T)(T rhs)

Implements comparison of Decimal with other numeric types and strings.

ref Decimal opUnary(string op)() 
if (op == "++")

Implements Decimal unary operator "++".

ref Decimal opUnary(string op)() 
if (op == "--")

Implements Decimal unary operator "--".

Decimal opUnary(string op)() 
if (op == "+")

Implements Decimal unary operator "+".

Decimal opUnary(string op)() pure const 
if (op == "-")

Implements Decimal unary operator "-".

Decimal opBinary(string op)(Decimal rhs) const 
if (op.among("+", "-", "*", "/", "^^"))

Implements binary operators between Decimals. Returns an object with the maximum precision of both original objects.

Decimal opBinary(string op, T)(T rhs) const 
if (op.among("+", "-", "*", "/", "^^"))

Implements binary operators between Decimal and build-in type values.

The precision of the decimal result is equal to the precision of the decimal argument.

Decimal opBinaryRight(string op, T)(T value) const 
if (op.among("+", "*", "-", "/", "^^"))

Implements operators with built-in integers on the left-hand side and Decimal on the right-hand side.

The precision of the decimal result is equal to the precision of the decimal argument.

Decimal opOpAssign(string op, T)(T value) 
if (op.among("+", "-", "*", "/", "^^"))

Implements assignment operators of the form Decimal op= integer: "+=", "-=", "*=", "/=", "^^=". Position of the fixed point (precision) doesn't change.

bool isInteger()

Check if Decimal has zero fractional part.

bool isZero()

Check if Decimal has zero value.