I have a table with datafields in real datatype. Do you lose precision when converting to money or decimal datatype?
views:
524answers:
2That 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.
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.