Skip to content

Instantly share code, notes, and snippets.

@omonien
Created November 23, 2022 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omonien/50c932567bb9be4d1af641193a6bcf62 to your computer and use it in GitHub Desktop.
Save omonien/50c932567bb9be4d1af641193a6bcf62 to your computer and use it in GitHub Desktop.
program CurrencyTests;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
begin
try
// Type Currency is an Integer internally and its ToString routines are optimized for money values
// https://docwiki.embarcadero.com/Libraries/Sydney/en/System.Currency
// Currency has a max precision of 4, if you pass in a value with 5 decimals, then an implicit conversion from
// float to currency is performed, which includes a round operation
var
b: Currency := 3.6199;
Assert(b * 10000 = 36199); // Integer comparison
Assert(Trunc(b) = 3); // Integer comparison
b := 3.61999; // Right side is a float constant (!!!)
Assert(b * 10000 = 36200); // Integer comparison
Assert(Trunc(b) = 3); // Integer comparison
//String tests
b := 3.6199;
// ToString is determined by Format settings
// FormatSettings are locale dependent - we want a testable setup here
// https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TFormatSettings.CurrencyDecimals
FormatSettings.CurrencyDecimals := 4; // Default is 2 - max is 4
// https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TFormatSettings.CurrencyFormat
FormatSettings.CurrencyFormat := 0; // $123
// https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TFormatSettings.CurrencyString
FormatSettings.CurrencyString := '$';
FormatSettings.DecimalSeparator := '.';
var
s := b.ToString;
Assert(s = '$3.6199');
except
on E: Exception do
writeln(E.ClassName, ': ', E.Message);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment