MongoDB C++ Driver mongocxx-4.0.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.
EXPECT(d128{}.to_string() == "0E-6176");
EXPECT(d128{"0"}.to_string() == "0");
EXPECT(d128{"12"}.to_string() == "12");
EXPECT(d128{"-76"}.to_string() == "-76");
EXPECT(d128{"12.70"}.to_string() == "12.70");
EXPECT(d128{"+0.003"}.to_string() == "0.003");
EXPECT(d128{"017."}.to_string() == "17");
EXPECT(d128{".5"}.to_string() == "0.5");
EXPECT(d128{"4E+9"}.to_string() == "4E+9");
EXPECT(d128{"0.73e-7"}.to_string() == "7.3E-8");
EXPECT(d128{"Inf"}.to_string() == "Infinity");
EXPECT(d128{"-infinity"}.to_string() == "-Infinity");
EXPECT(d128{"NaN"}.to_string() == "NaN");
// "NaN8275" is not a supported string representation.
EXPECT(d128{"123"}.to_string() == "123");
EXPECT(d128{"-123"}.to_string() == "-123");
EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3");
EXPECT(d128{"1.23E+5"}.to_string() == "1.23E+5");
EXPECT(d128{"12.3"}.to_string() == "12.3");
EXPECT(d128{"0.00123"}.to_string() == "0.00123");
EXPECT(d128{"1.23E-8"}.to_string() == "1.23E-8");
EXPECT(d128{"-1.23E-10"}.to_string() == "-1.23E-10");
EXPECT(d128{"0"}.to_string() == "0");
EXPECT(d128{"0.00"}.to_string() == "0.00");
EXPECT(d128{"0E+2"}.to_string() == "0E+2");
EXPECT(d128{"-0"}.to_string() == "-0");
EXPECT(d128{"0.000005"}.to_string() == "0.000005");
EXPECT(d128{"0.0000050"}.to_string() == "0.0000050");
EXPECT(d128{"5E-7"}.to_string() == "5E-7");
EXPECT(d128{"Infinity"}.to_string() == "Infinity");
EXPECT(d128{"-Infinity"}.to_string() == "-Infinity");
EXPECT(d128{"NaN"}.to_string() == "NaN");
// "NaN123" is not a supported string representation.
// "-sNaN" is not a supported string representation.
EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3");
EXPECT(d128{"123E+3"}.to_string() == "1.23E+5");
EXPECT(d128{"12.3E-9"}.to_string() == "1.23E-8");
EXPECT(d128{"-123E-12"}.to_string() == "-1.23E-10");
EXPECT(d128{"700E-9"}.to_string() == "7.00E-7");
EXPECT(d128{"70"}.to_string() == "70");
EXPECT(d128{"0.00E+3"}.to_string() == "0E+1");
EXPECT(d128{"0"}.to_string() == "0");
EXPECT(d128{"0.00"}.to_string() == "0.00");
EXPECT(d128{"123"}.to_string() == "123");
EXPECT(d128{"-123"}.to_string() == "-123");
EXPECT(d128{"1.23E3"}.to_string() == "1.23E+3");
EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3");
EXPECT(d128{"12.3E+7"}.to_string() == "1.23E+8");
EXPECT(d128{"12.0"}.to_string() == "12.0");
EXPECT(d128{"12.3"}.to_string() == "12.3");
EXPECT(d128{"0.00123"}.to_string() == "0.00123");
EXPECT(d128{"-1.23E-12"}.to_string() == "-1.23E-12");
EXPECT(d128{"1234.5E-4"}.to_string() == "0.12345");
EXPECT(d128{"-0"}.to_string() == "-0");
EXPECT(d128{"-0.00"}.to_string() == "-0.00");
EXPECT(d128{"0E+7"}.to_string() == "0E+7");
EXPECT(d128{"-0E-7"}.to_string() == "-0E-7");
EXPECT(d128{"inf"}.to_string() == "Infinity");
EXPECT(d128{"+inFiniTy"}.to_string() == "Infinity");
EXPECT(d128{"-Infinity"}.to_string() == "-Infinity");
EXPECT(d128{"NaN"}.to_string() == "NaN");
EXPECT(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.
EXPECT((d128{0u, 0u}) == d128{});
// [0,0,0]
EXPECT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"});
// [1,0,0]
EXPECT((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"});
// [0,0,1]
EXPECT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"});
// [0,0,-1]
EXPECT((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"});
// [0,123,0]
EXPECT((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"});
// [1,123,0]
EXPECT((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"});
// [0,123,1]
EXPECT((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"});
// [1,123,1]
EXPECT((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"});
// [0,123,-1]
EXPECT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"});
// [1,123,-1]
EXPECT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"});
// [0,inf]
EXPECT((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"});
// [1,inf]
EXPECT((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"});
// [0,qNaN]
EXPECT((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"});
// [1,qNaN]: Negative NaN is string-represented as NaN.
EXPECT((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN");
// [0,sNaN]: Signaling NaN is string-represented as NaN.
EXPECT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN");
// [1,sNaN]: Negative Signaling NaN is string-represented as NaN.
EXPECT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN");
}

Error Handling

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