Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Delphi how do I determine when to use Real, Real48, Double or Single data types?

Most of my applications revolve around financial calculations involving payments and interest rate calculations. I'm looking to find out how to determine what Delphi data type is best to use.

If I'm using a database to store these values and I've defined the fields in that database to be a decimal value with two decimal places, which Delphi datatype is most compatible with that scenario?

Should I use a rounding formula in Delphi to format the results to two decimal places before storing the values in the database? If so what is a best practice for doing so?

like image 675
Michael Riley - AKA Gunny Avatar asked Sep 10 '11 17:09

Michael Riley - AKA Gunny


1 Answers

For such calculations, don't use floating point types like Real, Single or Double. They are not good with decimal values like 0.01 or 1234.995, as they must approximate them.

You can use Currency, a fixed point type, but that is still limited to 4 decimal places.

Try my Decimal type, which has 28-29 places and has a decimal exponent so it is ideal for such calculations. The only disadvantage is that it is not FPU supported (but written in assembler, nevertheless) so it is not as fast as the built-in types. It is the same as the Decimal type used in .NET (but a little faster) and quite similar to the one used on the Mac.

like image 144
Rudy Velthuis Avatar answered Oct 22 '22 12:10

Rudy Velthuis