「C#」ToStringで数値を指定桁数までゼロを埋める

書式
int 変数名 =値
変数名.ToString(指定桁数ゼロ);

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplicationSample
{
class Program
{
static void Main(string[] args)
{
//数値を6桁までゼロ埋め編集する
int cft = 456;
int tt = 100;
//数値を6桁までゼロ埋め編集する
string res1 = cft.ToString("000000");
string res2 = tt.ToString("000000");
//コンソールに出力
Console.WriteLine("結果1: " + res1);
Console.WriteLine("結果2: " + res2);
Console.ReadKey();
}
}
}
using System; using System.Text.RegularExpressions; namespace ConsoleApplicationSample { class Program { static void Main(string[] args) { //数値を6桁までゼロ埋め編集する int cft = 456; int tt = 100; //数値を6桁までゼロ埋め編集する string res1 = cft.ToString("000000"); string res2 = tt.ToString("000000"); //コンソールに出力 Console.WriteLine("結果1: " + res1); Console.WriteLine("結果2: " + res2); Console.ReadKey(); } } }
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplicationSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //数値を6桁までゼロ埋め編集する
            int cft = 456;
            int tt = 100;
            //数値を6桁までゼロ埋め編集する
            string res1 = cft.ToString("000000");
            string res2 = tt.ToString("000000");

            //コンソールに出力
            Console.WriteLine("結果1: " + res1);
            Console.WriteLine("結果2: " + res2);

            Console.ReadKey();
        }
    }
}

実行結果
結果1: 000456
結果2: 000100

C#

Posted by arkgame