To format currency using TAdvMoneyEdit in Delphi, configure the component’s specialized numeric properties at design-time or programmatically in code. Unlike a standard TEdit which requires manual text parsing, TAdvMoneyEdit automatically formats currency symbols, decimal places, and thousands separators based on your settings or the operating system’s regional defaults. 1. Primary Component Settings
You can control the default display behavior directly via the Object Inspector using these key properties:
EditType: This must be set to etMoney. This tells the control to allow numbers, decimal separators, thousands separators, and currency symbols.
Precision: Defines how many digits are allowed after the decimal point (typically set to 2 for most currencies).
Signed: A boolean property. Set to True if you want to allow negative currency inputs. 2. Formatting via Global Settings
TAdvMoneyEdit dynamically pulls its currency symbols and delimiters from Delphi’s global TFormatSettings. If you need to force a specific currency layout (e.g., forcing a Euro symbol € or a Dollar sign \(</code>) regardless of the user's local Windows settings, manipulate the format settings before the component initializes:</p> <p><code>procedure TForm1.FormCreate(Sender: TObject); begin // Set the core component requirements AdvMoneyEdit1.EditType := etMoney; AdvMoneyEdit1.Precision := 2; // Enforce a specific currency format system-wide for the application FormatSettings.CurrencyString := '\)’; // Defines the currency symbol FormatSettings.ThousandSeparator := ‘,’; // Thousands separator character FormatSettings.DecimalSeparator := ‘.’; // Decimal separator character // Controls symbol placement: 0 = ‘\(1', 1 = '1\)’, 2 = ‘\( 1', 3 = '1 \)’ FormatSettings.CurrencyFormat := 0; // Refresh the control text to apply changes AdvMoneyEdit1.Text := FloatToStr(AdvMoneyEdit1.FloatValue); end; Use code with caution. 3. Safely Assigning and Retrieving Currency Values
When writing financial data to a database or calculating values, never read from or write to the .Text property directly to avoid data truncation or string errors. Use the component’s internal floating-point values:
procedure TForm1.CalculateTaxButtonClick(Sender: TObject); var ItemPrice: Currency; TaxTotal: Currency; begin // Safe retrieval: Automatically converts the edited string to a number ItemPrice := AdvMoneyEdit1.FloatValue; // Perform calculation TaxTotal := ItemPrice0.15; // Safe assignment: Automatically updates and formats the text on screen AdvMoneyEdit2.FloatValue := TaxTotal; end; Use code with caution. 4. Handling Negative Currency Formats
If your application processes negative balances, Delphi provides a specific formatting property called NegCurrFormat. You can control whether negative currency looks like -\(1.00</code>, <code>(\)1.00), or \(-1.00</code>:</p> <p><code>// Example: Set negative formatting to use parentheses, e.g., (\)1.00) FormatSettings.NegCurrFormat := 0; Use code with caution.
Leave a Reply