Sander Versluys
Sander Versluys

Reputation: 74527

Do you lose precision when converting from real to money datatype in sql server?

I have a table with datafields in real datatype. Do you lose precision when converting to money or decimal datatype?

Upvotes: 1

Views: 1772

Answers (2)

dmh2000
dmh2000

Reputation: 683

Float(4) (aka real) and float(8) are IEEE 754 floating point types. It is a mistake to use them to represent monetary values. They are meant for calculations in which the values have a large dynamic range while being stored in a relatively small number of bytes. The use of floating point numbers and how precise and accurate they are is a science in itself and not easily explained.

Upvotes: 2

Guffa
Guffa

Reputation: 700910

That depends on the precision and scale that you have specified for the decimal type. If you for example have a decimal(19,2) and try to convert the real number 1.123, you will get the value 1.12.

The decimal type has higher precision than the real type, so it's capable of handling any real value that is within it's range without loosing precision.

The money type can hold the same data as a decimal(19,4).

The decimal and money types are exact types, while real is an approximate type. If you convert a real to a decimal you may see a value that is not exactly what you see in the real value, but that is because the real value is rounded when it's displayed and the decimal value is not.

Upvotes: 4

Related Questions