準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// 4 桁固定
int v1 = 12;
listBox1.Items.Add(String.Format("{0:D4}", v1));
listBox1.Items.Add(String.Format("{0:0000}", v1));
// 右詰め・左詰め
int v11 = 12;
listBox1.Items.Add(String.Format("{0, 4}", v11));
listBox1.Items.Add(String.Format("{0,-4}", v11));
// 小数点以下 1 桁固定
int v2 = 12;
listBox1.Items.Add(String.Format("{0:0.0}", v2));
// % 表記
double v3 = 0.125;
listBox1.Items.Add(String.Format("{0:0.0%}", v3));
// 3 桁区切り
int v4 = 15000;
listBox1.Items.Add(String.Format("{0:#,0}", v4));
// 浮動小数点
int v5 = 15000;
listBox1.Items.Add(String.Format("{0:E}", v5));
}
}
}
解説
数値をさまざまな書式で出力しています。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.03.20 Visual Studio 2015 Professional (C# 6.0)
