FloatToDecimal
Delphiは、浮動小数点(例Extended
)とCurrency
値をさらにフォーマットするための便利な構造に変換するというプロシージャを提供します。例えば:
FloatToDecimal(..., 1234567890.1234, ...);
あなたにあげる:
TFloatRec
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = 10
IsNegative: Boolean = True
ここExponent
で、小数点の左側の桁数を示します。
処理する必要のある特殊なケースがいくつかあります。
指数はゼロです
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = 0
IsNegative: Boolean = True
小数点の左側に数字がないことを意味します。例:.12345678901234
指数が負です
Digits: array[0..20] of Char = "12345678901234"
Exponent: SmallInt = -3
IsNegative: Boolean = True
小数点と最初の桁の間にゼロを配置する必要があることを意味します。例:.00012345678901234
指数は-32768
(NaN、数値ではありません)
Digits: array[0..20] of Char = ""
Exponent: SmallInt = -32768
IsNegative: Boolean = False
値が数値ではないことを意味します。例:NAN
指数は32767
(INF、または-INF)です
Digits: array[0..20] of Char = ""
Exponent: SmallInt = 32767
IsNegative: Boolean = False
値が正または負の無限大(IsNegative
値に応じて)であることを意味します。例:-INF
FloatToDecimal
ロケールに依存しない「画像コード」の文字列を作成するための開始点として使用できます。
次に、この文字列を適切なWindowsGetNumberFormat
またはGetCurrencyFormat
関数に渡して、実際の正しいローカリゼーションを実行できます。
私は自分CurrToDecimalString
で書いたFloatToDecimalString
もので、数値を必要なロケールに依存しない形式に変換します。
class function TGlobalization.CurrToDecimalString(const Value: Currency): string;
var
digits: string;
s: string;
floatRec: TFloatRec;
begin
FloatToDecimal({var}floatRec, Value, fvCurrency, 0{ignored for currency types}, 9999);
//convert the array of char into an easy to access string
digits := PChar(Addr(floatRec.Digits[0]));
if floatRec.Exponent > 0 then
begin
//Check for positive or negative infinity (exponent = 32767)
if floatRec.Exponent = 32767 then //David Heffernan says that currency can never be infinity. Even though i can't test it, i can at least try to handle it
begin
if floatRec.Negative = False then
Result := 'INF'
else
Result := '-INF';
Exit;
end;
{
digits: 1234567 89
exponent--------^ 7=7 digits on left of decimal mark
}
s := Copy(digits, 1, floatRec.Exponent);
{
for the value 10000:
digits: "1"
exponent: 5
Add enough zero's to digits to pad it out to exponent digits
}
if Length(s) < floatRec.Exponent then
s := s+StringOfChar('0', floatRec.Exponent-Length(s));
if Length(digits) > floatRec.Exponent then
s := s+'.'+Copy(digits, floatRec.Exponent+1, 20);
end
else if floatRec.Exponent < 0 then
begin
//check for NaN (Exponent = -32768)
if floatRec.Exponent = -32768 then //David Heffernan says that currency can never be NotANumber. Even though i can't test it, i can at least try to handle it
begin
Result := 'NAN';
Exit;
end;
{
digits: .000123456789
^---------exponent
}
//Add zero, or more, "0"'s to the left
s := '0.'+StringOfChar('0', -floatRec.Exponent)+digits;
end
else
begin
{
Exponent is zero.
digits: .123456789
^
}
if length(digits) > 0 then
s := '0.'+digits
else
s := '0';
end;
if floatRec.Negative then
s := '-'+s;
Result := s;
end;
、、のエッジケースを除いて、これらNAN
の文字列をWindowsに渡すことができます。INF
-INF
class function TGlobalization.GetCurrencyFormat(const DecimalString: WideString; const Locale: LCID): WideString;
var
cch: Integer;
ValueStr: WideString;
begin
Locale
LOCALE_INVARIANT
LOCALE_USER_DEFAULT <--- use this one (windows.pas)
LOCALE_SYSTEM_DEFAULT
LOCALE_CUSTOM_DEFAULT (Vista and later)
LOCALE_CUSTOM_UI_DEFAULT (Vista and later)
LOCALE_CUSTOM_UNSPECIFIED (Vista and later)
}
cch := Windows.GetCurrencyFormatW(Locale, 0, PWideChar(DecimalString), nil, nil, 0);
if cch = 0 then
RaiseLastWin32Error;
SetLength(ValueStr, cch);
cch := Windows.GetCurrencyFormatW(Locale, 0, PWideChar(DecimalString), nil, PWideChar(ValueStr), Length(ValueStr));
if (cch = 0) then
RaiseLastWin32Error;
SetLength(ValueStr, cch-1); //they include the null terminator /facepalm
Result := ValueStr;
end;
FloatToDecimalString
と実装は読者のGetNumberFormat
練習問題として残されています(実際にはまだfloatを記述していないので、通貨だけです。指数表記をどのように処理するかわかりません)。
そしてボブのヤーおじさん。Delphiで適切にローカライズされたフロートと通貨。
整数、日付、時刻、および日時を適切にローカライズする作業はすでに完了しています。
注:すべてのコードはパブリックドメインにリリースされます。アトリビューションは必要ありません。