Created
November 23, 2022 18:22
-
-
Save omonien/50c932567bb9be4d1af641193a6bcf62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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