.NET SQL Float rounding problem

  • Thread starter Thread starter JCardinal
  • Start date Start date
J

JCardinal

Hello, I've run into something strange, I have the following SQL parameter
(c#):

cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@_rate",
System.Data.SqlDbType.Float));

This is for a simple stored procedure that updates a float value in a SQL
server 2000 (MSDE) table.

The following line of code:
cmd.Parameters["@_rate"].Value=1.33F;

Results in 1.33000004291534 being stored in the record.

It's obviously a binary rounding issue, but I can't seem to pin it down, I
honestly don't think it should be doing that.

Is it my parameter definition or...?
 
You can define the float and precision when you declare the parameter:
<Coming from memory and VB.. sorry>
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@_rate",
System.Data.SqlDbType.Float, 18, 5));
 
For this just need to change at the stored procedure level.
the parameter should be of Decimal(18,2)

Hope this helps to you.
thank
srinivas moorthy
-----Original Message-----
Hi Morgan, tried that, no difference at all.
It's really got me stumped and I can find no mention of it on the net, but
I'm doing everything "by the book" as far as I can tell.


Morgan said:
You can define the float and precision when you declare the parameter:
<Coming from memory and VB.. sorry>
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@_rate",
System.Data.SqlDbType.Float, 18, 5));
following SQL
parameter float value in a
SQL
server 2000 (MSDE) table.

The following line of code:
cmd.Parameters["@_rate"].Value=1.33F;

Results in 1.33000004291534 being stored in the record.

It's obviously a binary rounding issue, but I can't
seem to pin it down,
I

.
 
JCardinal said:
Hello, I've run into something strange, I have the following SQL parameter
(c#):

cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@_rate",
System.Data.SqlDbType.Float));

This is for a simple stored procedure that updates a float value in a SQL
server 2000 (MSDE) table.

The following line of code:
cmd.Parameters["@_rate"].Value=1.33F;

Results in 1.33000004291534 being stored in the record.

It's obviously a binary rounding issue, but I can't seem to pin it down, I
honestly don't think it should be doing that.

Yes it should. Float is an approximate data type. It's really only for
scientific measurements and stuff like that. You should probably be using an
exact datatype like Decimal(18,2).

David
 
Back
Top