精度指定子を持つ "D" 標準数値書式指定文字列 を使用して、整数に先行ゼロを追加できます。 カスタム数値書式指定文字列を使用して、整数と浮動小数点数の両方に先行ゼロを追加できます。 この記事では、両方のメソッドを使用して、先頭にゼロを付けて数値を埋め込む方法について説明します。
特定の長さになるまで整数値に先行ゼロを埋め込むには
整数値を表示する最小桁数を決定します。 この番号に先頭の数字を含めてください。
整数を 10 進値または 16 進数の値として表示するかどうかを決定します。
整数を 10 進値として表示するには、その
ToString(String)
メソッドを呼び出し、文字列 "Dn" をformat
パラメーターの値として渡します。ここで、 n は文字列の最小長を表します。整数を 16 進数の値として表示するには、
ToString(String)
メソッドを呼び出し、書式パラメーターの値として文字列 "Xn" を渡します。 ここで、n は文字列の最小長を表します。
C# と Visual Basic の両方で挿入文字列で書式指定文字列を使用することもできます。 または、複合書式を使用するString.FormatやConsole.WriteLineなどのメソッドを呼び出すことができます。
次の使用例は、書式設定された数値の長さが 8 文字以上になるように、複数の整数値を先頭に 0 で書式設定します。
byte byteValue = 254;
short shortValue = 10342;
int intValue = 1023983;
long lngValue = 6985321;
ulong ulngValue = UInt64.MaxValue;
// Display integer values by calling 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
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 calling 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
特定の数の先行ゼロを整数値に埋め込むには
整数値を表示する先頭のゼロの数を決定します。
整数を 10 進数または 16 進数の値として表示するかどうかを決定します。
それを 10 進値として書式設定するには、"D" 標準書式指定子が必要です。
それを 16 進数の値として書式設定するには、"X" 標準書式指定子が必要です。
整数値の
ToString("D").Length
またはToString("X").Length
メソッドを呼び出して、パッドなしの数値文字列の長さを確認します。書式設定された文字列に含める先頭のゼロの数を、パッドなし数値文字列の長さに加算します。 結果は、埋め込まれた文字列の合計の長さになります。
整数値の
ToString(String)
メソッドを呼び出し、10 進文字列の場合は文字列 "Dn" を、16 進文字列の場合は "Xn" を渡します。 n は埋め込まれた文字列の合計長を表します。 複合書式指定をサポートするメソッドでは、"Dn" または "Xn" 書式指定文字列を使用することもできます。
次の例では、5 つの先行ゼロを使用して整数値を埋め込みます。
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
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
特定の長さになるまで数値に先行ゼロを埋め込むには
文字列形式の数値の整数部分の桁数を決定します。 この数値の総桁数に先行ゼロを含めます。
ゼロプレースホルダー ("0") を使用してゼロの最小数を表すカスタム数値書式指定文字列を定義します。
数値の
ToString(String)
メソッドを呼び出し、カスタム書式指定文字列を渡します。 文字列補間または複合書式指定をサポートするメソッドでカスタム書式指定文字列を使用することもできます。
次の例では、複数の数値を先頭に 0 で書式設定します。 その結果、書式設定された数値の合計の長さは、10 進数の左側の少なくとも 8 桁になります。
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(dblValue.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
// 9034521202.93
//
// 01053240
// 00103932.52
// 01549230
// 9034521202.93
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(dblValue.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
' 9034521202.93
'
' 01053240
' 00103932.52
' 01549230
' 9034521202.93
特定の数の先行ゼロを数値に埋め込むには
数値に含める先行ゼロの数を決定します。
パッドなし数値文字列の小数点の左側の桁数を決定します。
数値の文字列表現に小数点記号が含まれているかどうかを判断します。
小数点記号が含まれている場合は、小数点の左側の文字数を決定します。 小数点記号が含まれていない場合は、文字列の長さを決定します。
次のものを使用するカスタム書式指定文字列を作成します。
- 文字列に表示する各先行ゼロに対するゼロ プレースホルダー ("0")。
- 既定の文字列内の各桁を表す 0 個のプレースホルダーまたは数字プレースホルダー "#" のいずれか。
数値の
ToString(String)
メソッドまたは複合書式指定をサポートするメソッドに、カスタム書式指定文字列をパラメーターとして指定します。
次の例では、2 つの Double 値に先頭に 5 つのゼロを埋め込みます。
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
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
こちらも参照ください
.NET