Save float valur with a stored procedure adds some decimal numbers

  • Hi!

    I'm having a strange behaviour with a stored procedure that has some parameters in float format.

    I used:

    float x = (float)1.5;

    float y= (float)4.3;

    ...

    row["x"] = x;

    row["y"] = y;

    I passed both parameters as this:

    cmd.Parameters.Add("@y", SqlDbType.Float).Value = (float)row["x"];

    cmd.Parameters.Add("@x", SqlDbType.Float).Value = (float)row["y"];

    And when I check in the data saved in database, it appears like this:

    x = 1,5

    y = 4,30000019073486

    Anyone knows why this happens?

    Regards

  • That's because float and real are not exact numeric data types, they're approximate numerics and can generate different values than the ones intended.

    Data Types (Transact-SQL)

    float and real (Transact-SQL)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks!

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

You must be logged in to reply to this topic. Login to reply