Michael
Michael

Reputation: 13626

How to store value in MSSQL 2008 with high precision?

I have Double type value in VisualStudio side .

I need to store this value in MSSQL 2008 in float column type.

The problem is that precision of my value is seventeen digits after the point,

in SQL side the few last digits is cut and rounded.

Any idea how to make SQL server to keep value as is (i.e. not to make changes in last digits)?

Thank you in advance!

Upvotes: 0

Views: 814

Answers (4)

Jodrell
Jodrell

Reputation: 35716

The .Net Double and the MSSQL server float are approximate numeric data types.

They offer an efficient way of storing "real" numbers. Internally they are stored as a two exact binary numbers, the base (matissa) and a multiplier (exponent). Thier precision varies based on the relative size of these two exact binary numbers, especially the mantissa. Operations using these floating point numbers are optimised on modern processors, especially GPUs.

At the small end of the number there is not a binary value for every real number in the range. When the respective ratios used to store the numbers differ, these gaps are interlaced and you get small rounding errors converting the numbers between ratios. With floating point numbers thats just the way it is. When floating point numbers are used to represent natural data a quintillionth either way doesn't make much difference.

If you really need the extra precision you should use the MSSQL decimal or numeric and the .Net Decimal type. However, they use more space and have lower performance than floating points.

Upvotes: 2

Bernhard Hofmann
Bernhard Hofmann

Reputation: 10411

You won't get precision from a FLOAT type because float is an approximate data type

Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly

You could use decimal.

Upvotes: 1

hgulyan
hgulyan

Reputation: 8249

Use Decimal data type instead of float.

Here's an example

DECIMAL(30, 17)

30 = maximum total number of decimal digits

17 = maximum number of decimal digits that can be stored to the right of the decimal point.

More on this

http://msdn.microsoft.com/en-us/library/aa258832(v=sql.80).aspx

Upvotes: 3

ederbf
ederbf

Reputation: 1713

FLOAT is not exact. For high precision, you should store them as DECIMAL / NUMERIC on the DB.

Upvotes: 1

Related Questions