none
Request and float parameter RRS feed

  • Question

  • Hello,
    I've a problem when i execute a request. This is my command :
    Code Snippet

    cmd.CommandText = "INSERT INTO Devices(...., CPP, ....) values (...,@CPP,...)";
    DbParameter myParameter;
    myParameter = cmd.CreateParameter();
    myParameter.ParameterName = "@CPP";
    myParameter.DbType = DbType.Single;
    myParameter.Direction = ParameterDirection.Input;
    myParameter.Value = 0.3;
    cmd.Parameters.Add(myParameter);

    After Executing this command the value in the database is not 0.3 but 0.30000001192092896!!!!
    When i do :
    Code Snippet

    cmd.CommandText = "INSERT INTO Devices(...., CPP, ....) values (...,0.3,...)";


    0.3 is correctly added in the database!

    I don't understand! i have tryied :
    Code Snippet

    myParameter.DbType = DbType.Double;


    and
    Code Snippet

    myParameter.Value = 0.3F;


    but nothing change...

    Thank you for yours answers!

    PS: Sorry for my english, i'm french
    Sunday, February 24, 2008 3:45 PM

All replies

  • Float and Double numbers are not exact and you might get some discrepancy. You might assign 1.0, but in reality double precision value would be 1.00000000001 or so, since all double and float numbers stored in compact 4 or 8 bytes format and to get them from storage applications or operating system applies specific algorithm and this is where precision discrepancy takes place.

     

    Monday, February 25, 2008 10:53 AM
    Moderator
  • Thanks, i have found the solution. The problem was that in SQL Server, a float is  a double in c#!

    Monday, February 25, 2008 11:09 AM
  • Or.. you can use Decimal in this case!
    Wednesday, May 28, 2014 11:49 AM