Why is a SQL float different from a C# float

后端 未结 6 612
清酒与你
清酒与你 2020-11-30 11:59

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

6条回答
  •  一个人的身影
    2020-11-30 12:43

    The reason it "doesn't feel right" is because C# uses the same syntax for unboxing and for casting, which are two very different things. exercise["DefaultAccelLimit"] contains a double value, boxed as an object. (double) is required to unbox the object back into a double. The (float) in front of that then casts the double to a float value. C# does not allow boxing and casting in the same operation, so you must unbox, then cast.

    The same is true even if the cast is nondestructive. If a float was boxed as an object that you wanted to cast into a double, you would do it like so: (double)(float)object_var.

提交回复
热议问题