Decimal value stored in MySql DB with floating point error
See original GitHub issueBug description
Decimal value stored with floating point error in mysqldb. Using a DECIMAL(20,10) field type in mysql and inserting the value 607013.31, in fact 607013.3100000001 is inserted. Checked the mysql logs and the error is already in the INSERT statement generated by prisma
How to reproduce
git clone https://github.com/unimatrix27/prismaissue
cd prismaissue
docker-compose up
This demo repo is a minimal environemt to demonstrate only this bug. Using docker-compose it sets up a mysql server, creates the demotable and runs NODE with the demoapp to show the bug
Expected behavior
The decimal value should be stored without floating point issues
Prisma information
prisma version 2.20, introspected DB, simplistic demo environment with no other code
Environment & setup
- OS: Docker NODE:15
- Database: MySql 5.7 (also tested 8.0)
- Node.js 15
- Prisma version: 2.20
const { PrismaClient } = require('@prisma/client')
var prisma = new PrismaClient({ log: ['query', 'info', 'warn'] })
var enterTestData = prisma.demotable.create({
data: {demofield: 607013.31}
}).then(r => {
console.log(r.demofield)
// this console.log results in 607013.3100000001
}
)
model demotable {
id Int @id @default(autoincrement()) @db.UnsignedInt
demofield Decimal? @db.Decimal(20, 10)
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
B.3.4.8 Problems with Floating-Point Values
Floating -point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an...
Read more >Floating Point Rounding Errors in MySQL - Navicat
Floating -point means the decimal point can be placed anywhere relative to the significant digits of the number with the actual position being ......
Read more >Floating point precision issue in PHP and MySQL for storing ...
Floats represent integers the way you want them, and they lose precision only when you store certain decimals. Share.
Read more >SQL FLOAT: Avoid Weird Math Errors with These Top 3 Points
Article explains how to avoid weird math errors when working with SQL FLOAT data type. Follow our easy examples!
Read more >Floating point numbers - Manual - PHP
So, if you add two floats, end up with a "special" value, e.g. 1.2E+6, then put that value unmodified into an update query...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
upgraded to 2.20 also in the test repo, same result
This is still super confusing. We’re having a meeting about floatish values tomorrow, hopefully we make it simpler to use in the future.
You’re not the first one having trouble… 😃