方法: 数値に先行するゼロを埋め込む

整数に先行するゼロを埋め込むには、精度指定子とともに "D" の標準の数値書式指定文字列を使用します。 整数および浮動小数点数に先行するゼロを埋め込むには、カスタム数値書式指定文字列を使用します。 このトピックでは、両方の方法を使用して、数値に先行するゼロを埋め込む方法について説明します。

先行するゼロを埋め込んで、整数を特定の長さにするには

  1. 表示する整数値の桁数を決定します。 この数値には、先行する桁数を含めます。

  2. 整数を、10 進値または 16 進値のどちらで表示するかを決定します。

    1. 整数を 10 進値で表示するには、その ToString(String) メソッドを呼び出し、文字列 "Dn" を format パラメーターの値として渡します。ここで、n は、文字列の最小の長さを表します。

    2. 整数を 16 進値で表示するには、その ToString(String) メソッドを呼び出し、文字列 "Xn" を format パラメーターの値として渡します。ここで、n は、文字列の最小の長さを表します。

    FormatWriteLine などの複合書式指定を使用するメソッドで書式指定文字列を使用することもできます。

書式指定された数値全体の長さが 8 文字以上になるように、先行するゼロをいくつかの整数値に付けて書式指定する例を次に示します。

Dim byteValue As Byte = 254
Dim shortValue As Short = 10342
Dim intValue As Integer = 1023983
Dim lngValue As Long = 6985321               
Dim ulngValue As ULong = UInt64.MaxValue

' Display integer values by caling the ToString method.
Console.WriteLine("{0,22} {1,22}", byteValue.ToString("D8"), byteValue.ToString("X8"))
Console.WriteLine("{0,22} {1,22}", shortValue.ToString("D8"), shortValue.ToString("X8"))
Console.WriteLine("{0,22} {1,22}", intValue.ToString("D8"), intValue.ToString("X8"))
Console.WriteLine("{0,22} {1,22}", lngValue.ToString("D8"), lngValue.ToString("X8"))
Console.WriteLine("{0,22} {1,22}", ulngValue.ToString("D8"), ulngValue.ToString("X8"))
Console.WriteLine()

' Display the same integer values by using composite formatting.
Console.WriteLine("{0,22:D8} {0,22:X8}", byteValue)
Console.WriteLine("{0,22:D8} {0,22:X8}", shortValue)
Console.WriteLine("{0,22:D8} {0,22:X8}", intValue)
Console.WriteLine("{0,22:D8} {0,22:X8}", lngValue)
Console.WriteLine("{0,22:D8} {0,22:X8}", ulngValue)
' The example displays the following output:
'                     00000254               000000FE
'                     00010342               00002866
'                     01023983               000F9FEF
'                     06985321               006A9669
'         18446744073709551615       FFFFFFFFFFFFFFFF
'       
'                     00000254               000000FE
'                     00010342               00002866
'                     01023983               000F9FEF
'                     06985321               006A9669
'         18446744073709551615       FFFFFFFFFFFFFFFF
byte byteValue = 254;
short shortValue = 10342;
int intValue = 1023983;
long lngValue = 6985321;               
ulong ulngValue = UInt64.MaxValue;

// Display integer values by caling the ToString method.
Console.WriteLine("{0,22} {1,22}", byteValue.ToString("D8"), byteValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", shortValue.ToString("D8"), shortValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", intValue.ToString("D8"), intValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", lngValue.ToString("D8"), lngValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", ulngValue.ToString("D8"), ulngValue.ToString("X8"));
Console.WriteLine();

// Display the same integer values by using composite formatting.
Console.WriteLine("{0,22:D8} {0,22:X8}", byteValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", shortValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", intValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", lngValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", ulngValue);
// The example displays the following output:
//                     00000254               000000FE
//                     00010342               00002866
//                     01023983               000F9FEF
//                     06985321               006A9669
//         18446744073709551615       FFFFFFFFFFFFFFFF
//       
//                     00000254               000000FE
//                     00010342               00002866
//                     01023983               000F9FEF
//                     06985321               006A9669
//         18446744073709551615       FFFFFFFFFFFFFFFF
//         18446744073709551615       FFFFFFFFFFFFFFFF

特定の数の先行するゼロを整数に埋め込むには

  1. 表示する整数値に先行するゼロの数を決定します。

  2. 整数を、10 進値または 16 進値のどちらで表示するかを決定します。 整数を 10 進値として書式指定するには、"D" 標準書式指子を使用する必要があります。16 進値として書式指定するには、"X" 標準書式指定子を使用する必要があります。

  3. 整数値の ToString("D").Length メソッドまたは ToString("X").Length メソッドを呼び出して、ゼロを埋め込んでいない数値文字列の長さを決定します。

  4. 書式指定された文字列に含める、先行するゼロの数を、埋め込まれない数値文字列の長さに加算します。 これで、ゼロを埋め込んだ文字列全体の長さが定義されます。

  5. 整数値の ToString(String) メソッドを呼び出して、10 進文字列の場合は文字列 "Dn" を、16 進文字列の場合は "Xn" を渡します。ここで、n は、ゼロを埋め込んだ文字列全体の長さを表します。 "Dn" または "Xn" 書式指定文字列は、複合書式指定をサポートするメソッドでも使用できます。

整数値に先行する 5 個のゼロを埋め込む例を次に示します。

Dim value As Integer = 160934
Dim decimalLength As Integer = value.ToString("D").Length + 5
Dim hexLength As Integer = value.ToString("X").Length + 5
Console.WriteLine(value.ToString("D" + decimalLength.ToString()))
Console.WriteLine(value.ToString("X" + hexLength.ToString()))
' The example displays the following output:
'       00000160934
'       00000274A6      
int value = 160934;
int decimalLength = value.ToString("D").Length + 5;
int hexLength = value.ToString("X").Length + 5;
Console.WriteLine(value.ToString("D" + decimalLength.ToString()));
Console.WriteLine(value.ToString("X" + hexLength.ToString()));
// The example displays the following output:
//       00000160934
//       00000274A6      

先行するゼロを埋め込んで、数値を特定の長さにするには

  1. 文字列形式の数値に含まれる整数部分の桁数を決定します。 この合計桁数には、先行するゼロをすべて含めます。

  2. ゼロの最小数を表すゼロ プレースホルダー ("0") を使用するカスタム数値書式指定文字列を定義します。

  3. 数値の ToString(String) メソッドを呼び出し、そのメソッドにカスタム書式指定文字列を渡します。 カスタム書式指定文字列は、複合書式指定をサポートするメソッドと併用できます。

書式指定された数値全体の長さが 8 桁以上の整数になるように、先行するゼロを付けていくつかの数値を書式指定する例を次に示します。

Dim fmt As String = "00000000.##"
Dim intValue As Integer = 1053240
Dim decValue As Decimal = 103932.52d
Dim sngValue As Single = 1549230.10873992
Dim dblValue As Double = 9034521202.93217412

' Display the numbers using the ToString method.
Console.WriteLine(intValue.ToString(fmt))
Console.WriteLine(decValue.ToString(fmt))            
Console.WriteLine(sngValue.ToString(fmt))
Console.WriteLine(sngValue.ToString(fmt))            
Console.WriteLine()

' Display the numbers using composite formatting.
Dim formatString As String = " {0,15:" + fmt + "}"
Console.WriteLine(formatString, intValue)      
Console.WriteLine(formatString, decValue)      
Console.WriteLine(formatString, sngValue)      
Console.WriteLine(formatString, dblValue)      
' The example displays the following output:
'       01053240
'       00103932.52
'       01549230
'       01549230
'       
'               01053240
'            00103932.52
'               01549230
'          9034521202.93      
string fmt = "00000000.##";
int intValue = 1053240;
decimal decValue = 103932.52m;
float sngValue = 1549230.10873992f;
double dblValue = 9034521202.93217412;

// Display the numbers using the ToString method.
Console.WriteLine(intValue.ToString(fmt));
Console.WriteLine(decValue.ToString(fmt));           
Console.WriteLine(sngValue.ToString(fmt));
Console.WriteLine(sngValue.ToString(fmt));           
Console.WriteLine();

// Display the numbers using composite formatting.
string formatString = " {0,15:" + fmt + "}";
Console.WriteLine(formatString, intValue);      
Console.WriteLine(formatString, decValue);      
Console.WriteLine(formatString, sngValue);      
Console.WriteLine(formatString, dblValue);      
// The example displays the following output:
//       01053240
//       00103932.52
//       01549230
//       01549230
//       
//               01053240
//            00103932.52
//               01549230
//          9034521202.93      

特定の数の先行するゼロを数値に埋め込むには

  1. 数値に含める、先行するゼロの数を決定します。

  2. ゼロを埋め込んでいない数値文字列の整数部分の桁数を決定します。 この操作を行うには、次の手順を実行します。

    1. 文字列形式の数値に小数点記号を含めるかどうかを決定します。

    2. 小数点記号を含める場合は、整数部分の文字数を決定します。

      または

      小数点記号を含めない場合は、文字列の長さを決定します。

  3. 文字列に表示される先行するゼロごとにゼロ プレースホルダー ("0") を使用するカスタム書式指定文字列、およびゼロ プレースホルダーまたは既定の文字列内の各桁を表す桁プレースホルダー ("#") を使用するカスタム書式指定文字列を作成します。

  4. 数値の ToString(String) メソッドまたは、複合書式指定をサポートするメソッドのいずれかに対するパラメーターとしてカスタム書式指定文字列を指定します。

2 つの Double 値に先行する 5 個のゼロを埋め込む例を次に示します。

Dim dblValues() As Double = { 9034521202.93217412, 9034521202 }
For Each dblValue As Double In dblValues
   Dim decSeparator As String = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
   Dim fmt, formatString As String

   If dblValue.ToString.Contains(decSeparator) Then
      Dim digits As Integer = dblValue.ToString().IndexOf(decSeparator)
      fmt = New String("0"c, 5) + New String("#"c, digits) + ".##"
   Else
      fmt = New String("0"c, dblValue.ToString.Length)   
   End If
   formatString = "{0,20:" + fmt + "}"

   Console.WriteLine(dblValue.ToString(fmt))
   Console.WriteLine(formatString, dblValue)
Next
' The example displays the following output:
'       000009034521202.93
'         000009034521202.93
'       9034521202
'                 9034521202            
double[] dblValues = { 9034521202.93217412, 9034521202 };
foreach (double dblValue in dblValues)
{
   string decSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
   string fmt, formatString;

   if (dblValue.ToString().Contains(decSeparator))
   {
      int digits = dblValue.ToString().IndexOf(decSeparator);
      fmt = new String('0', 5) + new String('#', digits) + ".##";
   }
   else
   {
      fmt = new String('0', dblValue.ToString().Length);   
   }
   formatString = "{0,20:" + fmt + "}";

   Console.WriteLine(dblValue.ToString(fmt));
   Console.WriteLine(formatString, dblValue);
}
// The example displays the following output:
//       000009034521202.93
//         000009034521202.93
//       9034521202
//                 9034521202            

参照

概念

カスタム数値書式指定文字列

標準の数値書式指定文字列

複合書式設定