Why not to use a FLOAT to represent a currency or other financial value

Many thanks to Berrie Roelofs

Query:
Why-not-to-use-floats-query

Results:
Why-not-to-use-floats-results

Just a small difference between the totals, but it’s there 🙂

More info on MSDN:

The decimal data type stores an exact representation of the number; there is no approximation of the stored value.

The float and real data types are known as approximate data types. The behavior of float and real follows the IEEE 754 specification on approximate numeric data types.
Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value. For many applications, the tiny difference between the specified value and the stored approximation is not noticeable. At times, though, the difference becomes noticeable. Because of the approximate nature of the float and real data types, do not use these data types when exact numeric behavior is required, such as in financial applications, in operations involving rounding, or in equality checks. Instead, use the integer, decimal, money, or smallmoney data types.

Download the T-SQL query:
Click here

Another example by Joseph Fluckiger can be found here.


Advertisements

One thought on “Why not to use a FLOAT to represent a currency or other financial value

  1. Pingback: Why not to use a MONEY data type to represent a currency or other financial value | Clint Huijbers' Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s