「C#」ToStringで数値を指定桁数までゼロを埋める
書式
int 変数名 =値
変数名.ToString(指定桁数ゼロ);
使用例
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