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.
我需要编写一个Delphi应用程序,从数据库中的各个表中提取条目,不同的条目将使用不同的货币。因此,我需要为每种货币数据类型($,Pounds,Euros等)显示不同数量的小数位和不同的货币字符,具体取决于我加载的商品的货币。
Is there a way to change the currency almost-globally, that is, for all Currency data shown in a form?
有没有办法几乎全局改变货币,也就是说,表格中显示的所有货币数据?
2 个解决方案
#1
7
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
即使使用相同的货币,您也可能必须使用不同的格式显示值(例如分隔符),因此我建议您仅将LOCALE与货币相关联而不是货币。您可以使用简单的Integer来保存LCID(区域设置ID)。请参阅此处的列表: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
5
I'd use SysUtils.CurrToStr(Value: Currency; var FormatSettings: TFormatSettings): string;
我使用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.
我设置了一个TFormatSettings数组,每个位置都配置为反映应用程序支持的每种货币。您需要为每个数组位置设置TFormat设置的以下字段:CurrencyString,CurrencyFormat,NegCurrFormat,ThousandSeparator,DecimalSeparator和CurrencyDecimals。
#1
7
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
即使使用相同的货币,您也可能必须使用不同的格式显示值(例如分隔符),因此我建议您仅将LOCALE与货币相关联而不是货币。您可以使用简单的Integer来保存LCID(区域设置ID)。请参阅此处的列表: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
5
I'd use SysUtils.CurrToStr(Value: Currency; var FormatSettings: TFormatSettings): string;
我使用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.
我设置了一个TFormatSettings数组,每个位置都配置为反映应用程序支持的每种货币。您需要为每个数组位置设置TFormat设置的以下字段:CurrencyString,CurrencyFormat,NegCurrFormat,ThousandSeparator,DecimalSeparator和CurrencyDecimals。