问题
I need to write a Delphi application that pulls entries up from various tables in a database, and different entries will be in different currencies. Thus, I need to show a different number of decimal places and a different currency character for every Currency data type ($, Pounds, Euros, etc) depending on the currency of the item I've loaded.
Is there a way to change the currency almost-globally, that is, for all Currency data shown in a form?
回答1:
Even with the same currency, you may have to display values with a different format (separators for instance), so I would recommend that you associate a LOCALE instead of the currency only with your values.
You can use a simple Integer to hold the LCID (locale ID).
See the list here: http://msdn.microsoft.com/en-us/library/0h88fahh.aspx
Then to display the values, use something like:
function CurrFormatFromLCID(const AValue: Currency; const LCID: Integer = LOCALE_SYSTEM_DEFAULT): string;
var
AFormatSettings: TFormatSettings;
begin
GetLocaleFormatSettings(LCID, AFormatSettings);
Result := CurrToStrF(AValue, ffCurrency, AFormatSettings.CurrencyDecimals, AFormatSettings);
end;
function USCurrFormat(const AValue: Currency): string;
begin
Result := CurrFormatFromLCID(AValue, 1033); //1033 = US_LCID
end;
function FrenchCurrFormat(const AValue: Currency): string;
begin
Result := CurrFormatFromLCID(AValue, 1036); //1036 = French_LCID
end;
procedure TestIt;
var
val: Currency;
begin
val:=1234.56;
ShowMessage('US: ' + USCurrFormat(val));
ShowMessage('FR: ' + FrenchCurrFormat(val));
ShowMessage('GB: ' + CurrFormatFromLCID(val, 2057)); // 2057 = GB_LCID
ShowMessage('def: ' + CurrFormatFromLCID(val));
end;
回答2:
I'd use SysUtils.CurrToStr(Value: Currency; var FormatSettings: TFormatSettings): string;
I'd setup an array of TFormatSettings, each position configured to reflect each currency your application supports. You'll need to set the following fields of the TFormat Settings for each array position: CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator and CurrencyDecimals.
来源:https://stackoverflow.com/questions/86002/in-delphi-how-can-you-have-currency-data-types-shown-in-different-currencies-in