在Delphi中,如何使货币数据类型以不同的货币以不同的形式显示?

时间:2020-03-05 18:59:34  来源:igfitidea点击:

我需要编写一个Delphi应用程序,该应用程序从数据库中的各个表中提取条目,并且不同的条目将采用不同的币种。因此,我需要为每种Currency数据类型($,Pounds,Euros等)显示不同的小数位数和不同的货币字符,具体取决于我加载的商品的货币。

是否有一种方法可以在几乎全局范围内更改货币,即以表格形式显示的所有Currency数据?

解决方案

回答

我会使用SysUtils.CurrToStr(Value:Currency; var FormatSettings:TFormatSettings):string;

我将设置一个TFormatSettings数组,每个位置配置为反映应用程序支持的每种货币。我们需要为每个数组位置设置" TFormat设置"的以下字段:CurrencyString,CurrencyFormat,NegCurrFormat,ThousandSeparator,DecimalSeparator和CurrencyDecimals。

回答

即使使用相同的货币,我们可能也必须显示不同格式的值(例如,分隔符),因此我建议我们将LOCALE而不是仅将货币与值相关联。
我们可以使用简单的Integer来保存LCID(语言环境ID)。
请参阅此处的列表:http://msdn.microsoft.com/zh-cn/library/0h88fahh.aspx

然后要显示值,请使用类似以下内容的方法:

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;