Decimal128 (High-Precision Decimal)
Last updated: 2026-10-09
Decimal128 (High-Precision Decimal)
Decimal128 is MongoDB's high-precision decimal type, based on the IEEE 754-2008 decimal floating-point standard, with 34 significant decimal digits. It suits money, rates, and other data needing exact arithmetic, avoiding Double's binary rounding errors. Construct with `Decimal128('19.99')` or the mongosh 5.0+ shorthand `NumberDecimal('19.99')`.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
Decimal128("<decimal string>")
NumberDecimal("<decimal string>") // mongosh 5.0+
{ price: Decimal128("19.99") }
Returns:Returns a Decimal128 BSON value
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// Decimal128: construct and exact match
use shop
db.prices.insertOne({ item: 'apple', price: Decimal128('19.99') })
db.prices.find({ price: { $eq: Decimal128('19.99') } })
Output:
{ _id: ObjectId('...'), item: 'apple', price: Decimal128('19.99') }
📌 Notes
- 34 significant decimal digits — far more precise than Double
- Use Decimal128 for all money arithmetic to avoid accumulated rounding
- Decimal128 and Double are distinct types; $eq between them is false
- Convert to string in aggregation with $toString for output
🏷️ Related Tags
💡 Related Cases
Documents and BSON: The Cornerstone of MongoDB Data
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
Mongoose Models and Schema Design
📚 Related Tutorials
❓ FAQ
QWhat is the difference between Decimal128 and Double?
ADouble is binary floating point where 0.1 is inexact; Decimal128 stores decimal digits (34 significant) for exact money math.
QHow large a value can Decimal128 hold?
AExponent range is roughly ±6143, value range about ±9.99×10^6144 — beyond any business need.
QWhy store prices as Decimal128?
AMoney needs exact decimal arithmetic; Double sums drift (e.g. 0.30000000000000004), breaking reconciliation.