BSON_DECIMAL128_T(3)

🌸 BSON Decimal128 Abstraction 🌸

okay bestie, so this is the BSON_DECIMAL128_T(3) page from libbson β€” it's giving "tiny struct, huge personality" and honestly it ate πŸ’…


✨ SYNOPSIS ✨

first things first, you gotta pull in the header, no cap:

#include <bson/bson.h>

then boom, these defines are your besties:

#define BSON_DECIMAL128_STRING 43
#define BSON_DECIMAL128_INF "Infinity"
#define BSON_DECIMAL128_NAN "NaN"

and here's the actual struct, look at her πŸ‘‘

typedef struct {
#if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN
   uint64_t low;
   uint64_t high;
#elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN
   uint64_t high;
   uint64_t low;
#endif
} bson_decimal128_t;

πŸ’– DESCRIPTION πŸ’–

so the bson_decimal128_t structure is literally the IEEE-754 Decimal128 data type, periodt. she's an aggregate that holds two uint64_ts, named high and low β€” iconic duo fr fr 🫢

here's the tea though: the declaration and layout order between them depends on the endian order of the target platform. and the way that works is:

  • low will always correspond to the low-order bits of the Decimal128 object 🌸
  • high corresponds to the high-order bits ⭐

and like, the bson_decimal128_t always has a size of sixteen (16) β€” yes, always β€” and can be bit-cast to/from a _Decimal128. no notes, she's just built different πŸ’€βœ¨


πŸŽ€ EXAMPLE πŸŽ€

here's a whole little program, go off:

#include <bson/bson.h>
#include <stdio.h>

int
main (int argc, char *argv[])
{
   char string[BSON_DECIMAL128_STRING];
   bson_decimal128_t decimal128;

   bson_decimal128_from_string ("100.00", &decimal128);
   bson_decimal128_to_string (&decimal128, string);
   printf ("Decimal128 value: %s\n", string);

   return 0;
}

πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc β€” the main characters, honestly 🧚


2009-present, MongoDB, Inc. πŸ’œ


okay that's the whole vibe check bestie, hope this slayed for you πŸ¦‹

and since we can't forget the footer: this page is from libbson version 2.5.6, dated Oct 09, 2026 β€” screenshot it, bookmark it, tape it to your mirror, whatever works πŸ’–πŸŽ€βœ¨
πŸ“¦ where this page comes from