Skip to content

Decimal Type having precision loss when retrieving value from database #1675

Open

Description

@adityashFT

Software versions

  • Tedious: 18.6.1
  • SQL Server: 12.0.2000.8
  • Node.js: 22.11.0

Additional Libraries Used and Versions
Sequelize: 6.37.5

Problem description
In our Database Table, we have a column of type Decimal(38,20). When saving the value 500, the record in the table is correct, however the data that is returned by is 499.99999999999994

When investigating, I have come across this Issue (#678), which was raised 7 years ago. However, there seems to be no progress in addressing the issue, our team relays in the column being of type decimal, but the value returned by tedious is incorrect, which are causing issues in the program.

Requesting a quick fix on this issue or a temporary workaround to fix the decimal precision issue. One way is to change the column type to varchar, which is not a solution we prefer.

Expected behavior
Value returned should be exactly 500

Actual behavior
Value returned should be exactly 499.99999999999994

Any other details that can be helpful
According to my analysis

return new Result((value * sign) / Math.pow(10, scale), offset);
This line seems to be causing the precision issue, but haven't deep-dived to check what is actual issue is.

Activity

  1. adityashFT commented on Jan 9, 2025

    @adityashFT
    Author

    Is there any update??

  2. KAMAELUA commented on Jan 15, 2025

    @KAMAELUA

    @adityashFT they have this issue since 2014 (#163). The easiest way is to fork this repo and fix that problem by yourself (as I do). You can try to use my fork if you want:

    npm install tedious-decimal
    

    and add the following params to connection options

    returnDecimalAndNumericAsString: true,
    decimalStringTrimTrailingZero: true,
    
  3. adityashFT commented on Jan 21, 2025

    @adityashFT
    Author

    @KAMAELUA , Yea its true.. and many library depend on the driver code. Hope that the team takes quick action to fix this long running issue

  4. adityashFT commented on Jan 21, 2025

    @adityashFT
    Author

    The problem is that I'm using sequelize. and this is not an ideal way to fix the issue.

  5. KAMAELUA commented on Jan 22, 2025

    @KAMAELUA

    @adityashFT I am using it with sequelize-typescript and it looks like you can use it the same way with sequelize:

    import {Sequelize} from "sequelize-typescript";
    import * as tedious from "tedious-decimal";
    
    const dialectModule = tedious;
    
    const sequelize = new Sequelize({
      dialect: 'mssql',
      dialectModule,
      dialectOptions: {
        options: {
          returnDecimalAndNumericAsString: true,
          decimalStringTrimTrailingZero: true,
        },
      }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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