MongoDB C++ Driver mongocxx-3.11.0
Loading...
Searching...
No Matches
Decimal128

How to use Decimal128 interfaces.

See also

Basic Usage

void example() {
using d128 = bsoncxx::decimal128; // For brevity.
// Default ctor.
ASSERT(d128{}.to_string() == "0E-6176");
ASSERT(d128{"0"}.to_string() == "0");
ASSERT(d128{"12"}.to_string() == "12");
ASSERT(d128{"-76"}.to_string() == "-76");
ASSERT(d128{"12.70"}.to_string() == "12.70");
ASSERT(d128{"+0.003"}.to_string() == "0.003");
ASSERT(d128{"017."}.to_string() == "17");
ASSERT(d128{".5"}.to_string() == "0.5");
ASSERT(d128{"4E+9"}.to_string() == "4E+9");
ASSERT(d128{"0.73e-7"}.to_string() == "7.3E-8");
ASSERT(d128{"Inf"}.to_string() == "Infinity");
ASSERT(d128{"-infinity"}.to_string() == "-Infinity");
ASSERT(d128{"NaN"}.to_string() == "NaN");
// "NaN8275" is not a supported string representation.
ASSERT(d128{"123"}.to_string() == "123");
ASSERT(d128{"-123"}.to_string() == "-123");
ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3");
ASSERT(d128{"1.23E+5"}.to_string() == "1.23E+5");
ASSERT(d128{"12.3"}.to_string() == "12.3");
ASSERT(d128{"0.00123"}.to_string() == "0.00123");
ASSERT(d128{"1.23E-8"}.to_string() == "1.23E-8");
ASSERT(d128{"-1.23E-10"}.to_string() == "-1.23E-10");
ASSERT(d128{"0"}.to_string() == "0");
ASSERT(d128{"0.00"}.to_string() == "0.00");
ASSERT(d128{"0E+2"}.to_string() == "0E+2");
ASSERT(d128{"-0"}.to_string() == "-0");
ASSERT(d128{"0.000005"}.to_string() == "0.000005");
ASSERT(d128{"0.0000050"}.to_string() == "0.0000050");
ASSERT(d128{"5E-7"}.to_string() == "5E-7");
ASSERT(d128{"Infinity"}.to_string() == "Infinity");
ASSERT(d128{"-Infinity"}.to_string() == "-Infinity");
ASSERT(d128{"NaN"}.to_string() == "NaN");
// "NaN123" is not a supported string representation.
// "-sNaN" is not a supported string representation.
ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3");
ASSERT(d128{"123E+3"}.to_string() == "1.23E+5");
ASSERT(d128{"12.3E-9"}.to_string() == "1.23E-8");
ASSERT(d128{"-123E-12"}.to_string() == "-1.23E-10");
ASSERT(d128{"700E-9"}.to_string() == "7.00E-7");
ASSERT(d128{"70"}.to_string() == "70");
ASSERT(d128{"0.00E+3"}.to_string() == "0E+1");
ASSERT(d128{"0"}.to_string() == "0");
ASSERT(d128{"0.00"}.to_string() == "0.00");
ASSERT(d128{"123"}.to_string() == "123");
ASSERT(d128{"-123"}.to_string() == "-123");
ASSERT(d128{"1.23E3"}.to_string() == "1.23E+3");
ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3");
ASSERT(d128{"12.3E+7"}.to_string() == "1.23E+8");
ASSERT(d128{"12.0"}.to_string() == "12.0");
ASSERT(d128{"12.3"}.to_string() == "12.3");
ASSERT(d128{"0.00123"}.to_string() == "0.00123");
ASSERT(d128{"-1.23E-12"}.to_string() == "-1.23E-12");
ASSERT(d128{"1234.5E-4"}.to_string() == "0.12345");
ASSERT(d128{"-0"}.to_string() == "-0");
ASSERT(d128{"-0.00"}.to_string() == "-0.00");
ASSERT(d128{"0E+7"}.to_string() == "0E+7");
ASSERT(d128{"-0E-7"}.to_string() == "-0E-7");
ASSERT(d128{"inf"}.to_string() == "Infinity");
ASSERT(d128{"+inFiniTy"}.to_string() == "Infinity");
ASSERT(d128{"-Infinity"}.to_string() == "-Infinity");
ASSERT(d128{"NaN"}.to_string() == "NaN");
ASSERT(d128{"-NAN"}.to_string() == "NaN");
// "SNaN" is not a supported string representation.
// "Fred" is not a supported string representation.
}

From Bytes

void example() {
using d128 = bsoncxx::decimal128; // For brevity.
// Default ctor.
ASSERT((d128{0u, 0u}) == d128{});
// [0,0,0]
ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"});
// [1,0,0]
ASSERT((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"});
// [0,0,1]
ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"});
// [0,0,-1]
ASSERT((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"});
// [0,123,0]
ASSERT((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"});
// [1,123,0]
ASSERT((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"});
// [0,123,1]
ASSERT((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"});
// [1,123,1]
ASSERT((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"});
// [0,123,-1]
ASSERT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"});
// [1,123,-1]
ASSERT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"});
// [0,inf]
ASSERT((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"});
// [1,inf]
ASSERT((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"});
// [0,qNaN]
ASSERT((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"});
// [1,qNaN]: Negative NaN is string-represented as NaN.
ASSERT((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN");
// [0,sNaN]: Signaling NaN is string-represented as NaN.
ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN");
// [1,sNaN]: Negative Signaling NaN is string-represented as NaN.
ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN");
}

Error Handling

void example() {
try {
bsoncxx::decimal128 d{"invalid"}; // Throws.
ASSERT(false && "should not reach this point");
} catch (const bsoncxx::exception& ex) {
ASSERT(ex.code() == bsoncxx::error_code::k_invalid_decimal128);
}
}