C# ToString()で数値の左側をゼロ埋めするサンプル

環境
Windows 10 Home 64bit
Microsoft Visual Studio Community 2022

構文
//num=対象の数値, n(Dの後ろ)=桁数
string result = num.ToString(“Dn");
ToString()は、数値の左側をDの後の桁数でゼロ埋めした文字列を返します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
public class Example
{
public static void Main()
{
int num = 9814568;
string result = num.ToString("D10");
Console.WriteLine(result);
}
}
using System; public class Example { public static void Main() { int num = 9814568; string result = num.ToString("D10"); Console.WriteLine(result); } }
using System;

public class Example
{   
      public static void Main()
      {
            int num = 9814568;
            
            string result = num.ToString("D10");
            
            Console.WriteLine(result);
 	}
}

実行結果
0009814568

C#

Posted by arkgame