c# object float field is different than database object float field - c#

using c# mvc4 and mssql I have an Object which has a float field, now when I look it in the database the float value is 2.014112E+17 but when I get the object in my code, it becomes 2.01411186E+17. why is it different between the object in the server and the object in the database? there is no conversion happening in between by me, just reading an object from database. Thank you
Edit: I'm using this float point as a timestamp to sync some of my data with another database and this issue is causing is me some problems, is there a way to get an accurate value or storing it as a float is a wrong idea in first place?

Floats are only accurate to a certain degree due to their implementation. For accuracy, use Decimal.
Difference between decimal, float and double in .NET?
float and double are floating binary point types. In other words, they
represent a number like this:
10001.10010110011
The binary number and the location of the binary point are both encoded within the value.
decimal is a floating decimal point type. In other words, they
represent a number like this:
12345.65789
Edit: You can also try saving the timestamp as a unix timestamp, which is just the number of seconds since 1970-01-01. It might be better suited for your needs

If you have literally used a SQL float with a C# float, these are not comparable. You should be using a SQL real to store your C# float.
See here for full chart: C# Equivalent of SQL Server DataTypes
As an aside, you will always have the potential for these issues when working with floating point numbers. Where possible, use a decimal instead.
Further reference for SQL float != C# float: Why is a SQL float different from a C# float

Related

Sql Server Float to C# double adding trailing zeros

I have a float column in SQL Server with value 21.261 , when I am fetching this column into c# double , using entity framework core 2.0 it is becoming 21.2610000000042, how to avoid this and get the exact value as 21.261
As you may have noticed , your number is probably not exactly 21.261, but only some pretty accurate approximation.
Depending on what your data really represents you could use a different database storage type like ‘DECIMAL’ ... that would typically help when storing monetary values, that have ‘cents’.
However, depending on the programming language being capable of working with rational numbers or fractions, the moment you would read in the data back into a float, you again have to deal with these issues and likewise have to deal with it during printing etc.

C# String to Float

When I convert a string to a float I get my answer plus a bunch of junk decimals. How do I fix this? Is this floats error I'm seeing?
string myFloat = "1.94";
float f = float.Parse(myFloat);
It needs to be a float for database reasons ...
By junk I mean 1.94 turns into: 1.94000005722046
The problem is that you can't use float / double if you want a precise representation of your parsed number. In case you need that you must use decimal. For money amounts its almost always required to use decimal. So keep that in mind.
Please read about how floating point numbers are represented internally:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
How 1.94 is represented internally by a float can be tested in this calculator:
http://pages.cs.wisc.edu/~rkennedy/exact-float?number=1.94
As you see its 1.940000057220458984375.
Databases support the decimal datatype:
Oracle offers DECIMAL: http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj15260.html
SQL Server offers DECIMAL: http://msdn.microsoft.com/de-de/library/ms187746.aspx
MySQL offers DECIMAL: https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
You can change it to a double to get a more accurate representation but if you need it exactly
You cannot fix this this is how floats are represented in computers. If you use the decimal data type you will get an exact representation.
string myFloat = "1.94";
decimal f = decimal.Parse(myFloat);
And then do the conversion to double when you store it to the database. You could still get some noise in the database. The only way to be sure to get rid of this is to use numeric in the database.

Convertion of textbox value to single adds extra precision in C#

While converting value of a textbox to Convert.ToSingle() adds some extra precision for values like 2.7 to 2.7999999523162842. I don't want any rounding of this value because I have to store the exact value entered by the user to DB. So if any one have this issue before please post the solution, any help will appreciated.
That's because a float is a binary fraction which cannot exactly represent the number 2.7 -- when you try to assign a number that is not a binary fraction to a float or a double, you will inevitably end up with an inexact representation.
You need use a decimal if you don't want to precise represent decimal numbers like 2.7.
var myDecimal = Convert.ToDecimal(input);
SImilarly, to store this value in a sql server database you need to use a SQL data type capable of storing precise decimal numerics. Conventiently, this data type is called decimal in SQL as well

What datatype to use when storing metric data in SQL databases?

I'm trying to store metric data (meters, kilometers, square-meters) in SQL Server 2012.
What is the best datatype to use? float (C#: double), decimal (C#: decimal) or even geometry? Or something different?
Either a decimal with an appropriate amount of precision for your data, or an int type, if appropriate
It completely depends on the application and what precision you need for it.
If we are talking about architecture then then precision needs are relatively limited and a C# 32-bit float will take you a long way. In SQL this translates to float(24), also referred to as the database type real. This SQL DB type requires 4 bytes of storage per entry.
If we instead want to address points on the surface of the earth you need a lot higher precision. Here a C# double is suitable, which corresponds to a SQL float(53) or just float. This SQL DB type requires 8 bytes of storage and should be used only if needed or if the application is small and disk/memory usage is not a concern.
The SQL Decimal is could be a good alternative for the actual SQL DB, but has 2 drawbacks:
It corresponds to a C# Decimal which is a type designed for financial usage and to prevent round-off errors. This design renders the C# Decimal type slower than a float/double when used in trigonometric methods etc. You could of course cast this back and forth in your code, but that is not the most straight-forward approach IMO.
"The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors." - MSDN : Decimal Structure
The SQL DB type Decimal requires 5-9 bytes of storage per entry (depending on the precision used), which is larger than the float(x) alternatives.
So, use it according to your needs. In your comment you state that its about real estate, so I'd go for float(24) (aka real) which is exactly 4 bytes and directly translates to a C# float. See: float and real (Transact-SQL)
Lastly, here is a helpful resource for converting different types between .Net and SQL: SqlDbType Enumeration
Depends what you want to do
float or double are non-exact datatypes (so 5.0 == 5.0 may be false due to rounding issues)
Decimal is an exact datatype (so 5.0 == 5.0 will always be true)
and Geometry/Geography (easy said) are for locations on a map.
Float calculation should be fastes among the three, since geography is binary data with some infomation about projection (ist all about maps here) and decimal technically not as easy to handle as float.

What's the correct data type I should use in this case?

I have to represent numbers in my database, which are amounts of chemical substances in food, like fats, energy, magnesium and others. These values are decimals in format 12345.67.
If I use decimal (5,2) as data type in SQL Server, it maps to Decimal type in Entity Framework. If I use float as data type in SQL Server, it maps to Double in Entity Framework.
I'm not sure what the best data type in SQL Server would have to be, or doesn't it really matter a lot?
EDIT - in my case it should be decimal(7,2), as mentioned in some of the remarks!
Thanks.
You need decimal(7,2)
7 is total number of digits
2 is after the decimal point
Differences:
float is approximate and will give unexpected results
decimal is exact
References:
Accuracy problem with floating numbers
SQL Server Float data type calculation vs decimal
DECIMAL(7,2) would be better than float - it's exactly what you need (5 + 2 digits). With floating types (eg. float, double) you may have some problems - e.g. with rounding.

Categories

Resources