Saturday, February 15, 2014

Currency Data Type in Delphi

Currency Data Type in Delphi

Currency is a fixed-point data type that minimizes rounding errors in monetary calculations. It is stored as a scaled 64-bit integer with the 4 least significant digits implicitly representing decimal places. When mixed with other real types in assignments and expressions, Currency values are automatically divided or multiplied by 10000.

The Currency type is designed for use in financial applications. It supports 4 decimal places with at least 53 bits precision. Decimal places beyond the supported 4 are rounded up or down, as appropriate. See the sample code for an example.

var
  account1, account2, account3 : Currency;
begin
  account1 := 123.456749;   // Too many decimals - will be rounded down
  account2 := 123.456750;   // Too many decimals - will be rounded up
  account3 := account1 + account2;

  ShowMessage('Account1 = '+CurrToStr(account1));
  ShowMessage('Account2 = '+CurrToStr(account2));
  ShowMessage('Account3 = '+CurrToStr(account3));
end;

Output

Account1 = 123.4567
Account2 = 123.4568
Account3 = 246.9135

No comments:

Post a Comment