What is the recommended way of storing currency? W...
# prisma-client
y
What is the recommended way of storing currency? Would normally use
Decimal
, but it loses its precision. Example: Value 97.85 turns into 97.8499999999999900
t
We convert all currency to cents and store as integers. So in your example we would store 97.85 as 9750 Quick division 9750/100 gets us back to 97.85. This is also how stripe.com handles it.
👍 1
y
Thanks for your help. I really appreciate it!!
t
Glad to help! Also, just noticed I made a typo in the math but I think you get the point 🙂
y
Was wondering why my entire machine blew up! 😉
🤣 2
@Tyler Bell This works for dealing with Prisma. But then the numbers get messed up when during the conversion from currency to integer! Example: 151.92 * 100 = 15191.999999999998 🤷‍♂️
t
Copy code
Math.round(100 * amount)
y
The amount of work just to get a correct decimal! 😄