2008-09-17 105 views
7

我需要編寫一個Delphi應用程序,從數據庫中的各個表中提取條目,不同的條目將使用不同的貨幣。因此,我需要爲每種貨幣數據類型($,磅,歐元等)顯示不同數量的小數位數和不同的貨幣字符,具體取決於我已加載的物品的貨幣。在Delphi中,如何以不同形式顯示不同貨幣的貨幣數據類型?

有沒有辦法改變貨幣幾乎是全球性的,也就是說,對於表單中顯示的所有貨幣數據?

回答

7

即使是同一種貨幣,你可以有不同的格式(分隔符爲例),顯示值,所以我建議你語言環境,而不是貨幣只與你的價值觀聯繫起來。
您可以使用簡單的整數來保存LCID(區域設置ID)。
在這裏看到名單:http://msdn.microsoft.com/en-us/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; 
5

我會用SysUtils.CurrToStr(價值:貨幣; VAR FormatSettings:TFormatSettings):字符串;

我會設置一個TFormatSettings數組,每個位置配置爲反映您的應用程序支持的每種貨幣。您需要爲每個數組位置設置TFormat設置的以下字段:CurrencyString,CurrencyFormat,NegCurrFormat,ThousandSeparator,DecimalSeparator和CurrencyDecimals。