NodeJS [SOLVED]: NodeJS: MySQL returns inaccurate decimal values

NodeJS [SOLVED]: NodeJS: MySQL returns inaccurate decimal values

Home Forums Scripting NodeJS Tutorials NodeJS [SOLVED]: NodeJS: MySQL returns inaccurate decimal values

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245859

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’ve a MySQL database storing decimal values (DECIMAL(32, 12)).
    When I select the values using HeidiSQL, the values are shown correctly (e.g. 15922.638440778302). But when I do the same in NodeJS using the MySQL binding (https://github.com/mysqljs/mysql), it shows me 15922.638440778303.

    My SQL-Query is pretty simple:

    SELECT SUM(`amount`) FROM `balances`;
    

    Any idea what can cause the difference? Maybe do I need to specify the precision during the MySQL initialization in NodeJS? At the moment, I don’t specify anything other than the login credentials:

    let mysql = require('mysql').createPool({
      host: global.dbconfig['dbhost'],
      database: global.dbconfig['dbname'],
      user: global.dbconfig['dbuser'],
      password: global.dbconfig['dbpass']
    })
    

    #245860

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Add below lines to your mysql config.

     supportBigNumbers: true
     bigNumberStrings: true
    

    Then it becomes:

    let mysql = require('mysql').createPool({
      host: global.dbconfig['dbhost'],
      database: global.dbconfig['dbname'],
      user: global.dbconfig['dbuser'],
      password: global.dbconfig['dbpass'],
      supportBigNumbers: true,
      bigNumberStrings: true,
    })
    

    More info on connection options: docs

    Source: https://stackoverflow.com/questions/48028240/nodejs-mysql-returns-inaccurate-decimal-values
    Author: gokcand
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.