Skip to content

losing decimal precision when updating table #985

@caeret

Description

@caeret

Issue description

losing decimal precision when updating table, no matter the argument is type float64 or string.

Example code

CREATE TABLE `demo` (
  id int not null primary key,
  amount decimal(27,18) not null
);
INSERT INTO `demo` values (1, 0);
package main

import (
	"database/sql"
	"fmt"
	_ "mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:knight+2019@tcp(192.168.35.100:3306)/test")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	for i := 0; i < 3; i++ {
		query(db, i, "1.123")
	}
}

func query(db *sql.DB, i int, v string) {
	_, err := db.Exec("UPDATE `demo` SET `amount` = `amount` + ?", v)
	if err != nil {
		panic(err)
	}

	row := db.QueryRow("select amount from demo")
	var amount string
	err = row.Scan(&amount)
	if err != nil {
		panic(err)
	}
	fmt.Println("i =", i, "and amount is", amount)
}

Error log

i = 0 and amount is 1.123000000000000000
i = 1 and amount is 2.246000000000000000
i = 2 and amount is 3.36899999999999980

Configuration

Driver version (or git SHA):
master

Go version: run go version in your console
go version go1.12.4 linux/amd64

Server version: E.g. MySQL 5.6, MariaDB 10.0.20
MySQL 5.7.25

Server OS: E.g. Debian 8.1 (Jessie), Windows 10
Ubuntu 16.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions