Decimal.this

Constructs a Decimal data type using the specified _value

struct Decimal(int bits)
@IEEECompliant("convertFormat", 22)
@IEEECompliant("convertFromDecimalCharacter", 22)
@IEEECompliant("convertFromHexCharacter", 22)
@IEEECompliant("convertFromInt", 21)
@IEEECompliant("decodeBinary", 23)
this
(
T
)
(
auto const ref T value
)
if (
bits == 32 ||
bits == 64
||
bits == 128
)

Parameters

value T

any integral, char, bool, floating point, decimal, string or character range _value Exceptions:

Data typeInvalidOverflowUnderflowInexact
integral
char
float
bool
decimal
string
range

Using integral values

auto a = decimal32(112);       //represented as 112 x 10^^0;
auto b = decimal32(123456789); //inexact, represented as 1234568 * x 10^^2

Using floating point values

auto a = decimal32(1.23);
//inexact, represented as 123 x 10^^-2, 
//because floating point data cannot exactly represent 1.23 
//in fact 1.23 as float is 1.230000019073486328125
auto b = decimal64(float.nan);

Using other decimal values

auto a = decimal32(decimal64(10)); 
auto b = decimal64(a);
auto c = decimal64(decimal128.nan);

Using strings or ranges

A _decimal value can be defined based on _decimal, scientific or hexadecimal representation:

  • values are rounded away from zero in case of precision overflow;
  • auto d = decimal32("2.3456789")
    //internal representation will be 2.345679
    //because decimal32 has a 7-digit precision
  • the exponent in hexadecimal notation is 10-based;
  • auto d1 = decimal64("0x00003p+21");
    auto d2 = decimal64("3e+21");
    assert (d1 == d2);
  • the hexadecimal notation doesn't have any _decimal point, because there is no leading 1 as for binary floating point values;
  • there is no octal notation, any leading zero before the decimal point is ignored;
  • digits can be grouped using underscores;
  • case insensitive special values are accepted: nan, qnan, snan, inf, infinity;
  • there is no digit count limit for _decimal representation, very large values are rounded and adjusted by increasing the 10-exponent;
  • auto d1 = decimal32("123_456_789_123_456_789_123_456_789_123"); //30 digits
    //internal representation will be 1.234568 x 10^^30
  • NaN payloads can be defined betwen optional brackets ([], (), {}, <>). The payload is unsigned and is accepted in decimal or hexadecimal format;
auto d = decimal32("10");              //integral
auto e = decimal64("125.43")           //floating point
auto f = decimal128("123.456E-32");    //scientific
auto g = decimal32("0xABCDEp+21");     //hexadecimal 0xABCD * 10^^21
auto h = decimal64("NaN1234");         //$(B NaN) with 1234 payload
auto i = decimal128("sNaN<0xABCD>")    //signaling $(B NaN) with a 0xABCD payload
auto j = decimal32("inf");             //infinity

Using char or bool values

These constructors are provided only from convenience, and to offer support for conversion function $(PHOBOS conv, to, to). Char values are cast to unsigned int. Bool values are converted to 0.0 (false) or 1.0 (true)

auto a = decimal32(true); //1.0
auto b = decimal32('a');  //'a' ascii code (97)

auto c = to!decimal32(false); //phobos to!(bool, decimal32)
auto d = to!decimal128('Z');  //phobos to!(char, decimal128)

Meta