使用c#.net进行求和运算中的前导零

时间:2016-01-28 11:03:07

标签: c#

示例:

int a = 0000000;
int b = 17;

int c = a + b;

c值应为0000017

如果b 223表示c应为0000223

5 个答案:

答案 0 :(得分:2)

您使用.ToString("D7")用于输出,请阅读本文How to: Pad a Number with Leading Zeros

中的更多内容
c.ToString("D7");

答案 1 :(得分:1)

前导零在字符串中有意义。你得到如下:

{ 
    "_id" : ObjectId("56a9eeeacdc5b3ea38c0a522"), 
    "Data" : {
        "str" : "thestring", 
        "byte" : 42,
        "bytearray" : BinData(0, "AQID"))
    }
}

答案 2 :(得分:1)

00000000相同的整数;如果您想要不同的String表示,请使用格式

  int a = 0000000; // equals to 0
  int b = 17;

  int c = a + b;

  Console.Write(c.ToString("D7"));  // 7 digits, "0000000" format will do the same

答案 3 :(得分:0)

您可以使用此方法:

using System;

public class Example
{
   public static void Main()
   {
    int a = 0000000;
    int b = 17;
    String s = String.Format("{0,10:0000000}",a+b);
    Console.WriteLine(s);
   }
}

输出:

0000017

答案 4 :(得分:0)

在应用程序中,前导零只有在显示时才有意义,这意味着它们实际上是strings时。 在数字形式中,您看不到,也不应该关注。

所以,不要关心int变量中的前导零。当您想要显示或以string使用c.ToString("D7")时使用它们时。当然,您可以更改为任意数量的数字,7仅适用于您的示例。