Decimal128

noun

say it: “DES-ih-mul wun-TWEN-tee-AYT” /ˈdɛsɪməl wʌn ˈtwɛnti ˈeɪt/

A MongoDB data type for exact decimal numbers, used for amounts of money.

What is Decimal128 in MongoDB?

Decimal128 is the BSON type for exact decimal numbers, so money adds up to the cent; an ordinary number can be off by a tiny fraction. In mongosh you write one as NumberDecimal("0.60"), with the value in quotes.

0.1 + 0.2;   // 0.30000000000000004 with ordinary numbers
db.recipes.updateOne({ _id: 4 }, { $set: { costPerServing: NumberDecimal("0.60") } });
db.recipes.findOne({ _id: 4 }).costPerServing;
// Decimal128('0.60')

In a sentence

  • “In mongosh, NumberDecimal("19.99") makes a Decimal128 value.”
  • “Prices are stored as Decimal128, so a cart of three $0.10 items totals exactly $0.30.”

Level: Journeyman · More MongoDB terms at this level

Updated