数値の文字幅をそろえる - 数値の先頭の桁を空白文字で埋めて桁揃えする - C#

数値の文字幅をそろえるコードを紹介します。

概要

数値の文字幅をそろえて出力したい場合、数値の先頭の桁をスペースで埋めて文字列にすることで桁揃えができます。 書式を指定して数値を文字列に変換するにはFormatメソッドを利用します。

書式

Formatメソッドの第一引数の書式設定文字列に下記の文字列を与えます。

下記の記述は、0番目のインデックスの値を幅4で表現します。
"{0,4:d}"
カスタム数値書式指定文字列を利用した場合の記述です。下記の記述は0番目のインデックスの値を幅8で表現します。
"{0,8:#}"

プログラム例

Windows Formアプリケーションを作成します。

UI

下図のUIを作成します。複数行のテキストボックスとボタンを4つ配置します。

コード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NumericFormatDemo
{
  public partial class FormSpaceFill : Form
  {
    public FormSpaceFill()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      int value = 250;
      textBox_Output.Text += string.Format("{0,8:d}\r\n", value);

      value = 8;
      textBox_Output.Text += string.Format("{0,8:d}\r\n", value);

      value = 18;
      textBox_Output.Text += string.Format("{0,8:d}\r\n", value);

      value = 15000;
      textBox_Output.Text += string.Format("{0,8:d}\r\n", value);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      int value = 18;
      textBox_Output.Text += string.Format("{0,4:00}\r\n", value);

      value = 4;
      textBox_Output.Text += string.Format("{0,4:00}\r\n", value);

      value = 9;
      textBox_Output.Text += string.Format("{0,4:00}\r\n", value);

      value = 0;
      textBox_Output.Text += string.Format("{0,4:00}\r\n", value);

      value = 5;
      textBox_Output.Text += string.Format("{0,4:d2}\r\n", value);

      value = 8;
      textBox_Output.Text += string.Format("{0,4:d2}\r\n", value);
    }

    private void button3_Click(object sender, EventArgs e)
    {
      int value = 250;
      textBox_Output.Text += string.Format("{0,6:#}\r\n", value);

      value = 8;
      textBox_Output.Text += string.Format("{0,6:#}\r\n", value);

      value = 18;
      textBox_Output.Text += string.Format("{0,6:#}\r\n", value);

      value = 15000;
      textBox_Output.Text += string.Format("{0,6:#}\r\n", value);
    }

    private void button4_Click(object sender, EventArgs e)
    {
      int value = 16800;
      textBox_Output.Text += string.Format("{0,6:#,#}\r\n", value);

      value = 3320;
      textBox_Output.Text += string.Format("{0,6:#,#}\r\n", value);

      value = 185;
      textBox_Output.Text += string.Format("{0,6:#,#}\r\n", value);

      value = 164822;
      textBox_Output.Text += string.Format("{0,6:#,#}\r\n", value);

      value = 1500000;
      textBox_Output.Text += string.Format("{0,6:#,#}\r\n", value);
    }
  }
}

解説

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


[button1]をクリックします。テキストボックスに下図の文字列が表示されます。8文字で長さが揃えられ、先頭の桁が空白文字で埋められていることがわかります。


[button2]をクリックします。テキストボックスに下図の文字列が表示されます。4文字で長さが揃えられ、数値は2桁で0埋めされていることが確認できます。


[button3]をクリックします。テキストボックスに下図の文字列が表示されます。4文字で長さが揃えられ表示できています。


[button4]をクリックします。テキストボックスに下図の文字列が表示されます。6文字で長さが揃えられ表示されます。 また、数値は3桁ごとにカンマ区切りの表示になっています。長さを指定した際の文字数は数値の数ではなくカンマも1文字として数えた長さで表示されていることがわかります。
(16,800 は数値としては5桁ですが、6文字の長さの文字列として処理されます。)


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-04-14
作成日: 2018-11-16
iPentec all rights reserverd.