Writing precise decimal data
Persisting monetary or other fixed-precision values in a relational database.
Prisma · Class
Prisma.Decimal is Prisma Client's Decimal-field value type, backed by Decimal.js. Construct it with a numeric value and use Decimal.js arithmetic methods before passing it to a query.
Prisma exposes Decimal fields through Prisma.Decimal, which is backed by Decimal.js. Construct a value with Prisma.Decimal and use Decimal.js methods such as plus before passing it to a Prisma Client query; MongoDB does not currently support Decimal fields.
new Prisma.Decimal(value)import { PrismaClient, Prisma } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
const sample = await prisma.sample.create({
data: { cost: new Prisma.Decimal(24.454545).plus(1) },
});
console.log(sample.cost.toString());
}
main().finally(() => prisma.$disconnect());Creates a Decimal value, performs exact Decimal.js arithmetic, and stores it in a Prisma Decimal field.
Persisting monetary or other fixed-precision values in a relational database.
Applying arithmetic before insertion or update.
Prisma.Decimal is the Prisma Client representation for schema fields of type Decimal. It is implemented with the Decimal.js library, so Decimal.js operations can be used on values before they are written.
Follow-up: How would you add one to a Decimal value? Construct it with Prisma.Decimal and call .plus(1) before supplying it to the query data.
Create a value with new Prisma.Decimal(value) and assign it to the Decimal field in the query data. The documentation demonstrates this with a create operation.
Follow-up: Can arithmetic be performed during construction of query data? Yes; the documented pattern is new Prisma.Decimal(24.454545).plus(1).
Decimal is not currently supported as a field when MongoDB is the datasource. Applications using MongoDB should choose a supported representation instead of declaring a Decimal field.
Follow-up: Where should connector-specific Decimal mappings be checked? Use the Prisma schema reference, which documents default mappings and native database types.
Decimal.jsPrismaClientDecimal fieldPrisma schema reference