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.

Constructors

this
this(string number, size_t fixedPointPosition)

Constucts an object from a decimal string.

this
this(Decimal number, size_t fixedPointPosition)

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

this
this(BigInt number, size_t fixedPointPosition)

Constucts an object from a BigInt object.

this
this(T number, size_t fixedPointPosition)

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

this
this(real number, size_t fixedPointPosition)

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

Members

Functions

abs
Decimal abs()

Gets the modulus.

getSign
int getSign()

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

isInteger
bool isInteger()

Check if Decimal has zero fractional part.

isZero
bool isZero()

Check if Decimal has zero value.

opBinary
Decimal opBinary(Decimal rhs)

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

opBinary
Decimal opBinary(T rhs)

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

opBinaryRight
Decimal opBinaryRight(T value)

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

opCast
T opCast()

Converts to a boolean value.

opCast
T opCast()

Converts to a floating type, if possible.

opCast
T opCast()

Converts to an integer type, if possible.

opCmp
int opCmp(Decimal rhs)

Implements comparison of Decimal with other Decimal.

opCmp
int opCmp(T rhs)

Implements comparison of Decimal with other numeric types and strings.

opEquals
bool opEquals(Decimal rhs)

Implements Decimal equality test with other Decimal.

opEquals
bool opEquals(T rhs)

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

opOpAssign
Decimal opOpAssign(T value)

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

opUnary
Decimal opUnary()

Implements Decimal unary operator "++".

opUnary
Decimal opUnary()

Implements Decimal unary operator "--".

opUnary
Decimal opUnary()

Implements Decimal unary operator "+".

opUnary
Decimal opUnary()

Implements Decimal unary operator "-".

round
Decimal round(size_t lastDigitPosition)

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

toFullString
string toFullString()

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.

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

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

Meta