如何:用前导零填充数字

 

通过结合使用“D”标准数字格式字符串和精度说明符,可以用前导零填充整数。 通过使用自定义数字格式字符串,可以用前导零填充整数和浮点数。 本主题说明如何使用这两种方法为数字填充前导零。

用前导零将整数填充为特定长度

  1. 确定整数值要显示的位数。 包括此数字中的任何前导位。

  2. 确定是要将整数显示为十进制值还是十六进制值。

    1. 若要将整数显示为十进制值,则调用其 ToString(String) 方法,并传递字符串“Dn”作为 format 参数的值,其中 n 表示字符串的最小长度。

    2. 若要将整数显示为十六进制值,则调用其 ToString(String) 方法,并传递字符串“Xn”作为 format 参数的值,其中 n 表示字符串的最小长度。

    也可以在使用复合格式的方法(例如 FormatWriteLine)中使用格式字符串。

下面的示例用前导零设置若干个整数值的格式,以使格式化数字的总长度至少有八个字符。

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. 确定是要将整数显示为十进制值还是十六进制值。 将整数格式化为十进制值需要使用“D”标准格式说明符;将整数格式化为十六进制值需要使用“X”标准格式说明符。

  3. 通过调用整数值的 ToString("D").LengthToString("X").Length 方法,确定未填充的数值字符串的长度。

  4. 将要在格式化字符串中包括的前导零数目与未填充数值字符串的长度相加。 这将定义已填充字符串的总长度。

  5. 调用整数值的 ToString(String) 方法,并传递适用于十进制字符串的“Dn”和适用于十六进制字符串的“Xn”,其中 n 表示已填充字符串的总长度。 也可以在支持复合格式的方法中使用“Dn”或“Xn”格式字符串。

下面的示例使用五个前导零来填充整数值。

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) 方法并为其传递自定义格式字符串。 也可以将自定义格式字符串与支持复合格式的方法一起使用。

下面的示例用前导零设置若干个数值的格式,以使格式化数字在小数点左边的总长度至少有八位。

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) 方法或一个支持复合格式的方法。

下面的示例使用五个前导零填充两个 Double 值。

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            


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值