quick question here (not sure if it’s the right pl...
# orm-help
j
quick question here (not sure if it’s the right place), but I’m having some issues using Decimals in Sqlite. I basically want to store percentage values with 2 digits behind the comma max.
👀 1
r
Hi @Jan D'Hollander 👋 To store Decimals in SQLite, you would use the Decimal datatype. An example is shown in this sample schema.
Copy code
model products {
  id  Int @id @default(autoincrement())
  price Decimal
}
From the client, you can do something like
Copy code
const product = await prisma.products.create({
      data: {
        price: 5.47
      }
    })
Logging the result, we get
Copy code
{ id: 2, price: 5.47 }
Viewing the Sqlite table in Vscode,
j
Hi there, so, I’m using this approach, however, when I seed data (that’s formatted to 2 digits behind the comma), I still get very long numbers
Prisma shows me
8.800000000000001
for a number
8.80
I seeded
I create the decimal like this:
const number = new Number(decimal.toFixed(2));
the output I get in a console log is,
8.80
but the value stored is very long
I can’t reproduce this consistently, is just sometimes “happen”
it’s funny, when this happens, it’s usually with values around certain numbers
but Sqlite doesn’t allow me to set a formatter on my schema
@Raphael Etim ☝️ any idea?
j
Floating point cannot store arbitrary decimals, it gets as close as it possibly can
8.8 is an unrepresentable number
so you are storing 8.80000000000000198951966012828052043914794921875
(or something like that)
Play around with floats in JS to see:
j
Hmm I see, what would be the best way to store percentage values?