osanger
osanger

Reputation: 2362

EF6 inserting float values

I want to store a percentage value in my database built with EF6.

so i defined:

[Column(TypeName = "float")]
[Range(0, 1)]
public float RepeatedPointValuePercentage { get; set; }

If I insert 0.12 to the database the value in the table is 0.11999999731791

Code that inserts the value:

var o = new Object{ RepeatedPointValuePercentage = 12f / 100;}
//add to dbset & save changes

How can i fix it without a migration?

Upvotes: 0

Views: 222

Answers (1)

Jonathan Magnan
Jonathan Magnan

Reputation: 11347

You should NEVER use float for accuracy wise. As @helper answered, this is how the float value is stored (An approximate-number data-type) and there is nothing you can do to fix it.

However, you can use DECIMAL instead which will handle this scenario.

Upvotes: 2

Related Questions