Delphi Programming / Object Pascal
サイトのトップページへ リンクのページへ ロゴマーク
[掲載 2019年10月24日] [更新 2019年10月24日] Delphi 一般・その他
Format 関数と FormatFloat 関数による書式文字列
動作確認等 Windows 7 U64(SP1) + Delphi XE5(UP2) Pro VCL-32
[整理番号 - 0630]







Format 関数と FormatFloat 関数による書式文字列
FormatDisp.zip [889 KB] 2019年10月24日版
結果の表示用に使用しているユニットと使用サンプル


Format 関数と FormatFloat 関数は、数値を書式設定して文字列として出力するための関数です。本ページはそれらの関数の簡単な使用例です。詳細はオンラインヘルプ等で確認してください。
本文中で「浮動小数点形式」と言う用語を使用していますが、これは、科学表記形式、あるいは指数形式と言うこともあります。



本ページのサンプルでは下のユニット内の関数を使用しています。これは出力の位置確認などのために使用しています。表示用のフォントは等幅フォントの MS ゴシック を使用しています。
このコードを FormatDisp.pas の名前で、ライブラリパスの通った場所に保存して、各サンプルでは uses に FormatDisp を追加して使用しています。



リスト1
unit FormatDisp;

interface

uses
  Winapi.Windows, System.SysUtils, Vcl.StdCtrls, System.StrUtils;

procedure ResultDisp(AMemo: TMemo; AFmtStr, AResStr: string);

implementation

procedure ResultDisp(AMemo: TMemo; AFmtStr, AResStr: string);
CONST
  LFmtText = '%-15s%-20s';
var
  LLineStr : string;
  LResStr : string;
begin
  LLineStr := DupeString('-', 35);
  if AMemo.Lines.IndexOf(LLineStr) < 0 then begin
    AMemo.Lines.Clear;
    LResStr := DupeString('1234567890', 2);
    AMemo.Lines.Add(Format(LFmtText, ['format text', lResStr]));
    AMemo.Lines.Add(LLineStr);
  end;

  AMemo.Lines.Add(Format(LFmtText, [AFmtStr, AResStr]));
end;

end.


[備考]
書式が必要なければ IntToStr, UIntToStr, FloatToStgr 関数が使用できます。
符号付き整数型は IntToStr 関数を使用します。符号なし整数型は UIntToStr 関数を使用します。実数型の数値には FloatToStr 関数を使用します。これらの関数は、単一の数値変数用です。




01_[ Format 関数 ]  複数の数値と複数の書式の型


Format 関数では、% の文字に続けて d 等の書式の型を指定します。
下図は書式の型として d だけを使用した例です。d は符号付きの整数値用の書式の型です。結果の図の左側が使用した書式の文字列、右側が出力された書式の文字列です。

数値は Format 関数の 2 番目の引数の [ ] 内にカンマで区切って複数指定できますが、その分の書式も必要です。 下図では 1234 という数値を出力しています。書式文字列内の空白文字はそのまま出力されます。



図1
% の文字の後に書式の型の文字を書く
  • 図では書式の型として d を使用している
  • 出力する数値は複数指定できる
  • その分の書式が必要

リスト2
//=============================================================================
//  d は整数値用
//  書式も数値も複数指定可能。数値はカンマで区切る
//  空白はそのまま出力される
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Integer;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12345;

  LFmtText := '%d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%d%d';
  LResText := Format(LFmtText, [LValue, LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%d%d%d';
  LResText := Format(LFmtText, [LValue, LValue, LValue ]);
  ResultDisp(Memo1, LFmtText, LResText);

  // 書式ごとに空白文字を入れないと区別がつかない
  LFmtText := '%d %d  %d';
  LResText := Format(LFmtText, [LValue, LValue, LValue ]);
  ResultDisp(Memo1, LFmtText, LResText);
end;


書式の文字列内の文字は、空白だけでなく、文字列もそのまま出力されます。下図はその例です。
ただし、漢字があると挙動が怪しくなるようです。筆者にはこの現象についての知識はありません。


図2
書式文字列内の文字列はそのまま出力される

リスト3
//=============================================================================
//  d は整数値用
//  書式文字列内の文字列は、空白も含めてそのまま出力される
//  日本語の文字があると挙動が怪しくなるようだが、詳細は不明
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Integer;
  LFmtText : string;
  LResText : string;
begin
  LValue := 1234567;

  LFmtText := '%d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'str=%d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'str=%d TEST';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'str=%d 文字';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '文字= %d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;


各書式の型に対して出力の幅を設定することができます。幅は % の後に書きます。幅を指定すると出力は右端寄せとなります。幅の数字の前に - (マイナス) を付加すると左端寄せとなります。
下図では数値 1234 を文字列として出力していますが、数字のない部分は空白文字で埋まります。

実数値用の書式の型にはこの他に、小数部の桁数、あるいは仮数部の桁数を設定する精度指定子があります。使用方法は後述のサンプルで確認してください。



図3
出力幅の指定
  • 数字がない部分は空白文字

リスト4
//=============================================================================
//  d は整数値用
//  桁数を指定すると右端寄せ
//  桁数の前に - を書くと左端寄せ
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Integer;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12345;

  LFmtText  := '%d';
  LResText  := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%2d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'str=%10d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'str=%-10d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;


Format 関数では % は書式指定に使用する記号です。この % を文字として出力するには、下図のように % を 2 つ続けて記述します。


図4
% を文字として出力する方法
  • % を 2 つ続けて書く

リスト5
//=============================================================================
//  d は整数値用
//  % を書式指定子でなく、文字として出力するには % を 2 つ続けて書く
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Integer;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12345;

  LFmtText := '%d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%%%d';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%d%%';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%d%%%%';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




02_[ Format 関数 ]  s は文字列用


書式の型 s は文字列用です。他の数値用の書式の型と同じように幅が指定できます。幅を指定すると右端寄せとなります。幅指定の数字の前に - を付けると左端寄せとなります。
幅の値は文字数となります。したがって、下図の 3 番目と 4 番目は 7 文字てすが、漢字 3 文字は半角 6 文字相当なので、結果的に半角相当では 10 文字になります。

漢字の場合、書式文字列の中に文字列を書くと出力位置が正しく反映されないようですが、s 書式を使用して出力すると出力位置が反映されるようです。



図5
書式型 s は文字列用

リスト6
//=============================================================================
//  s は文字列用
//  文字率の幅指定は文字単位 (半角も全角も 1 文字が幅 1)
//  幅を指定すると右端寄せ
//  幅の値の前に - を書くと左端寄せ
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : string;
  LFmtText : string;
  LResText : string;
begin
  LValue := '日本語';

  LFmtText := '%s';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%2s';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%7s';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%7s%10d';
  LResText := Format(LFmtText, [LValue, 12345]);
  ResultDisp(Memo1, LFmtText, LResText);

  LValue := 'Text';
  LFmtText := '%10s';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%-10s';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




03_[ Format 関数 ]  d は符号付き u は符号なし整数値用


書式の型 d と u は整数値用です。d は符号付き整数値用で、u は符号なし整数値用です。
符号付きの整数型の変数に対して u を使用したり、符号なし整数型の変数に d を使用したりすると正しい結果とならないことがあります。下図はその例です。符号付きの整数型である Int32 型の変数に負の値を代入して、書式 d と u で出力しています。

Format 関数では、この d と u と、整数値を 16 進数文字列で出力する x が整数値用です。



図6
d は符号付き整数値用、u は符号なし整数値用
  • Int32 型の数値 -12345 の場合
  • Int32 は符号付きの 32 ビット整数型

リスト7
//=============================================================================
//  d は符号付き整数値用
//  u は符号なし整数値用
//  負の値を u で出力すると違う値になる
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Int32;
  LFmtText : string;
  LResText : string;
begin
  LValue := -12345;

  LFmtText  := '%d';
  LResText  := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%u';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;


書式の型 d と u の精度指定子の値は整数部の桁数です。先行する 0 を出力します。ただし、出力可能な桁数は、対象の変数型で表現可能な桁数の範囲内に限ります。



図7
d と u の精度指定子は整数部の桁数
  • UInt32 型の数値 1234567 の場合
  • UInt32 は符号付なしの 32 ビット整数型
図8
d と u の精度指定子は整数部の桁数
  • UInt64 型の数値 1234567 の場合
  • UInt64 は符号付なしの 64 ビット整数型

リスト8
//=============================================================================
//  d と u における精度指定子は整数部の桁数
//  この精度指定子は先行する 0 を出力する
//  整数部の先行する 0 は、変数の整数型で表現可能な桁数内に限る
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : UInt32;
  LFmtText : string;
  LResText : string;
begin
  LValue := 1234567;

  LFmtText  := '%.3u';
  LResText  := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%.10u';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%.20u';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




04_[ Format 関数 ]  f と n は固定小数点形式で出力


書式の型 f と n は実数値用です。どちらも固定小数点形式の出力ですが、n は 3 桁毎に位取り記号のカンマを出力します。
小数部の桁数は精度指定子で設定します。小数部の桁数のデフォルトは 2 桁です。
下図は書式の型が f の例です。

f と n のどちらも 1.0E+18 以上になると浮動小数点形式の出力になるようです。絶対値が小さい値は固定小数点形式の出力のようです。



図9
書式型 f は実数値を固定小数点形式で出力
  • Double 型の数値 12345.6789 の例
  • %20.5f の 20 は幅指定、5 が精度指定子


書式の型が n の例です。3 桁毎に位取り記号を出力します。



図10
書式型 n は実数値を固定小数点形式で、3 桁毎に位取り記号のカンマを出力
  • Double 型の数値 12345.6789 の例
  • %20.5n の 20 は幅指定、5 が精度指定子

リスト9
//=============================================================================
//  f は実数値用で固定小数点の形式の出力
//  小数点部の桁数のデフォルトは 2 桁。精度指定子で設定可能
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12345.6789;

  LFmtText := '%f';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%20.5f';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




05_[ Format 関数 ]  e は浮動小数点形式で出力


書式の型 e は実数値用で、出力は浮動小数点形式になります。指数部は符号付き 3 桁の固定です。
精度指定子の値は仮数部の桁数です (仮数部には符号と小数点の記号はない)。


図11
書式型 e は実数値を浮動小数点形式で出力
  • Single 型の数値 12.3456 の例
  • %20.5e の 20 は幅指定、5 が精度指定子

上の結果の図をみると、小数点以下に誤差があります。同じ数値を Double 型にして実行すると下図の結果になります。


図12
書式型 e は実数値を浮動小数点形式で出力
  • Double 型の数値 12.3456 の例
  • %20.5e の 20 は幅指定、5 が精度指定子

リスト10
//=============================================================================
//  e は実数値用で浮動小数点の形式の出力
//  仮数部は常に 1 桁
//  指数部の桁数は 3 桁固定
//  仮数部の桁数は精度指定子で設定する
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Single;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12.3456;

  LFmtText := '%e';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%20.5e';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




06_[ Format 関数 ]  g は固定小数点または浮動小数点形式で出力


書式の型 g は実数値用で、出力は固定小数点形式と浮動小数点形式が自動で切り換わります。
10 の -5 乗以下か 10 の 5 乗以上の時は、浮動小数点形式での出力となります。その範囲外は固定小数点形式の出力となります。
仮数部の桁数を精度指定子で設定できます。



図13
書式型 g は固定小数点形式と浮動小数点形式が自動で切り換わる
  • Doublel 型の数値 12.3456789 例
  • %15.5g の 15 は幅指定、5 が精度指定子


数値を Double 型 12345.6789 にした場合です。この値は 10 の -5 乗以下の値なので、浮動小数点形式の出力となります。



図14
書式型 g は固定小数点形式と浮動小数点形式が自動で切り換わる
  • Doublel 型の数値 1.23456789E-5 の例
  • %15.5g の 15 は幅指定、5 が精度指定子

リスト11
//=============================================================================
//  g は実数値用で固定小数点形式または浮動小数点形式の出力
//  10 の -5 乗以下または 10 の 5 乗以上になると自動的に浮動小数点形式
//  浮動小数点形式の指数部は符号付きの 3 桁固定
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
begin
  LValue := 12.3456789;

  LFmtText := '%g';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%15.5g';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




07_[ Format 関数 ]  m は実数値を金額形式で出力


書式の型 m は実数値専用で、固定小数点形式の出力となります。
実数値を 3 桁毎に位取り記号のカンマを付けて、先頭に通貨記号を付けた金額形式で出力します。書式は [地域と言語] の通貨の設定を使用します。
通貨記号 ¥ (円記号) は日本と中国で使用されています。アメリカの通過記号は $ (ドル) です。
精度指定子は小数部の桁数です。デフォルトでは小数点以下は出力しません。



図15
書式型 m は実数値を金額形式で出力
  • Currency 型の数値 1234567890 の例
  • %20.1m の 20 は幅指定、1 が精度指定子

リスト12
//=============================================================================
//  m は実数値用で金額形式の出力
//  デフォルトでは小数点以下は出力しない
//  出力の形式は、関係グローバル変数の設定内容による
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Currency;
  LFmtText : string;
  LResText : string;
begin
  LValue := 1234567890;

  LFmtText := '%m';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%20.1m';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);
end;




08_[ Format 関数 ]  p はポインタ用でアドレス値を出力


ポインタ型の変数は変数の型情報とアドレス情報を持っています。書式の型 p は、そのアドレス情報の値を取得して 16 進数の文字列として出力します。汎用のポインタである Pointer 型の変数は型情報を持っていませんがアドレス値を取得して出力できます。
32 ビットの EXE ではアドレスは 32 ビットなので 8 桁の出力となります。64 ビットの EXE のアドレスは 64 ビットなので 16 桁の出力になります。

出力幅が指定できることと、出力の桁数が固定であることを除けば、ポインタを NativeInt でキャストして取得した値と同じです。



図16
書式型 p はポインタ用
  • 数値 12345.67890 を代入した Double 型の変数のポインタの例

リスト13
//=============================================================================
//  p は数値のポインタのアドレス値を 16 進数の文字列で出力
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
  LAddress : NativeInt;
begin
  LValue := 12345.67890;

  LFmtText := '%p';
  LResText := Format(LFmtText, [@LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%15p';
  LResText := Format(LFmtText, [@LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  Memo1.Lines.Add('');
  LAddress := NativeInt(@LValue);
  Memo1.Lines.Add('Address : ' + IntToHex(LAddress, 8));
  Memo1.Lines.Add(FloatToStr(PDouble(LAddress)^));
end;




09_[ Format 関数 ]  x は整数値の 16 進数文字列を出力


書式の型 x は整数値専用です。整数値の 16 進数値を文字列で出力します。精度指定子の値は、16 進数の桁数ではなく、出力の文字数です。
出力幅が指定できることと、精度指定子が文字の数であることをを除けば、IntToHex 関数で取得した値と同じです。



図17
書式型 x は整数値の 16 進数を文字列で出力
  • Integer 型の数値 123456 の例
  • %15.8x の 15 は幅指定、8 が精度指定子

リスト14
//=============================================================================
//  x は整数値の 16 進数を文字列で出力する
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Integer;
  LFmtText : string;
  LResText : string;
begin
  LValue := 123456;

  LFmtText := '%x';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '%15.8x';
  LResText := Format(LFmtText, [LValue]);
  ResultDisp(Memo1, LFmtText, LResText);

  Memo1.Lines.Add('');
  Memo1.Lines.Add(IntToHex(LValue, 8));
end;




10_[ FormatFloat 関数 ]  固定小数点形式の出力


FormatFloat 関数を使用して、数値を固定小数点形式で出力する例です。Double 型の数値 123.456 を表示しています。FormatFloat 関数で出力する桁数は書式指定子 0 と # で決まります。
FormatFloat 関数は、1 つの数値に対して書式を指定した 10 進数の文字列を出力する関数です。関数名に Float がありますが、整数値も使用できます。

指定子 0 では、指定子 0 を書いた桁数分を出力します。桁位置に数字がある場合はその数字になります。整数部の指定子 0 の数が不足していても整数部は全て出力します。したがって、整数部の 1 桁目だけ 0 にする書式にしておけば、整数部は全て出力されることになります。
指定子 # は数字のない余分な桁を出力しません。整数部の書式を #0 にしておくと、そのことが明確にになります。

概ね、10 の -18 以下、または 10 の 18 乗以上になると浮動小数点形式になります。
書式文字列内の文字列もそのまま出力しますが、漢字の場合は挙動が怪しいようです。



図18
FormatFloat 関数で固定小数点形式の出力
  • Double 型の数値 123.456 の例


下図は、書式指定子 0 と # の違いの確認用に、Double 型の数値 0.12345 を出力した結果です。



図19
FormatFloat 関数の書式指定子 0 と # の違い
  • 使用した数値は Double 型の 0.12345

リスト15
//=============================================================================
//  FormatFloat 関数
//  整数値でも使用可能
//
//  固定小数点形式の出力
//  固定小数点表示では出力できない範囲になると浮動小数点形式の出力となる
//  10 進数で約 18 桁 (10 の -18 以下または 10 の 18 以上)
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
begin
  LValue := 123.456;

  LFmtText := '000000.00000';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '0.00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '#0.00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := 'value= #0.00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText :=  '結果= #0.00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);
end;




11_[ FormatFloat 関数 ]  浮動小数点形式の出力


FormatFloat 関数を使用して、数値を浮動小数点形式で出力する例です。Double 型の数値 123400000 を表示しています。浮動小数点形式で出力するには書式指定子 E+ または E- を使用します。
E+ (または e+) にすると指数部の + 記号を出力します。E- (または e-) にすると、指数部の符号が + の時に + 記号を出力しません。



図20
図17 FormatFloat 関数で浮動小数点形式の出力
  • Double 型の数値 123400000 の例

リスト16
//=============================================================================
//  FormatFloat 関数
//  整数値でも使用可能
//  浮動小数点形式の出力
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
begin
  LValue := 123400000;

  LFmtText := '.00000E+';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '#.00000E+';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '0.00000E+';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '0.00000E+00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '0.00000E+000';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '00.0000E+000';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);
end;




12_[ FormatFloat 関数 ]  3 桁毎に位取り記号のカンマを付加


FormatFloat 関数で 3 桁毎に位取り記号のカンマを出力するには、書式指定子の 「,」 (カンマ) を使用します。書式指定子 # は余分な 0 を出力しませんから、整数部の書式を。#,##0 すると、そのことが明確にになります。
Format 関数の書式の型 n と m でも 3 桁毎に位取り記号のカンマを出力できますが、それらは実数値用です。FormatFloat 関数では整数値も使用できます。



図21
3 桁毎に位取り記号のカンマを出力
  • FormatFloat 関数は整数値も使用可能

リスト17
//=============================================================================
//  FormatFloat 関数
//  整数値でも使用可能
//  3 桁毎に位取り記号 ( カンマ ) を付加
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Uint64;
  LFmtText : string;
  LResText : string;
begin
  LValue := 123456789012;

  LFmtText := ',0';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := ',0.00';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);

  LFmtText := '#,##0';
  LResText := FormatFloat(LFmtText, LValue);
  ResultDisp(Memo1, LFmtText, LResText);
end;


先行する 0 を出力する場合は以下のように先頭に , (カンマ) を付けます。



図22
3 桁毎に位取り記号のカンマを出力
  • 先行する 0 を出力する場合は、先頭に , (カンマ) を付ける

リスト18
//=============================================================================
//  FormatFloat 関数
//  整数値でも使用可能
//  3 桁毎に位取り記号 ( カンマ ) を付加
//  先行する 0 を出力する場合は先頭に , (カンマ) を付ける
//=============================================================================
procedure TForm1.Button1Click(Sender: TObject);
var
  LValue   : Double;
  LFmtText : string;
  LResText : string;
begin
  Memo1.Lines.Clear;

  LValue := 12345678.123;

  LFmtText := ',0000000000000000000000000000.00000';
  LResText := FormatFloat(LFmtText, LValue);

  Memo1.Lines.Add('Format Text');
  Memo1.Lines.Add(LFmtText);

  Memo1.Lines.Add('');
  Memo1.Lines.Add('out put');
  Memo1.Lines.Add(LResText);
end;