Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a SQL float different from a C# float

Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I am accessing a column that is defined in SQL as a float datatype. I am trying to assign that value to a local variable (c# float datatype) but am getting an InvalidCastExecption

DataRow exercise = _exerciseDataSet.Exercise.FindByExerciseID(65); _AccelLimit = (float)exercise["DefaultAccelLimit"];   

Now, playing around with this I did make it work but it did not make any sense and it didn't feel right.

_AccelLimit = (float)(double)exercise["DefaultAccelLimit"]; 

Can anyone explain what I am missing here?

like image 335
Keith Sirmons Avatar asked Sep 23 '08 17:09

Keith Sirmons


People also ask

What is a float in SQL?

Float is an approximate number data type used to store a floating-point number. float (n) - n is the number of bits that are used to store the mantissa in scientific notation. Range of values: - 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308. Storage size: 4 Bytes if n = 1-9 and 8 Bytes if n = 25-53 – default = ...

Does SQL use float?

Float & Real Data Types in SQL Server uses the floating-point number format. Real is a Single Precision Floating Point number, while Float is a Double Precision Floating Point number. The Floating point numbers can store very large or very small numbers than decimal numbers.

What is the precision of float in SQL?

float [ (n) ] Where n is the number of bits that are used to store the mantissa of the float number in scientific notation and, therefore, dictates the precision and storage size. If n is specified, it must be a value between 1 and 53. The default value of n is 53.

What is SQL float in C#?

The float in Microsoft SQL Server is equivalent to a Double in C#. The reason for this is that a floating-point number can only approximate a decimal number, the precision of a floating-point number determines how accurately that number approximates a decimal number.


2 Answers

A SQL float is a double according to the documentation for SQLDbType.

like image 130
Austin Salonen Avatar answered Sep 21 '22 17:09

Austin Salonen


A float in SQL is a Double in the CLR (C#/VB). There's a table of SQL data types with the CLR equivalents on MSDN.

like image 24
bdukes Avatar answered Sep 19 '22 17:09

bdukes