显示带前导零的数字文本框值

pre*_*thi 3 c#

我有3个文本框值:

no of requests = 2
court fee = 60
claim amount = 200
Run Code Online (Sandbox Code Playgroud)

我的客户要求他们希望展示:

no of requests = 00002; // 5 characters
court fee = 000006000; // 9 characters
claim amount = 0000020000; // 10 characters
Run Code Online (Sandbox Code Playgroud)

我试过这个,但没有得到这些价值观.我不知道我哪里错了.

decimal requests = 0;
decimal CFee = 0;
decimal CLAIMAMT = 0;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    CFee += Convert.ToDecimal(dataGridView1.Rows[i].Cells["CFee"].Value) / 100 * dataGridView1.RowCount;
    CLAIMAMT += Convert.ToDecimal(dataGridView1.Rows[i].Cells["CLAIMAMT"].Value) / 100 ;
    requests = Convert.ToInt16(dataGridView1.RowCount.ToString());
}

textBox3.Text = CFee.ToString();//court fee
textBox4.Text = CLAIMAMT.ToString();//claim amoiunt
textBox2.Text = requests.ToString();//no 
Run Code Online (Sandbox Code Playgroud)

jmc*_*ney 9

不要对整数使用小数.使用int表示整数,小数表示货币金额等.如果你想要前导零,那么你可以这样做:

var str = number.ToString("00000"); // At least five digits with leading zeroes if required.
Run Code Online (Sandbox Code Playgroud)

  • 或者,他们可以使用 `D` 格式说明符:`ToString("D5")`、`ToString("D9")`、`ToString("D10")` 等(假设他们改用 `int` 而不是比“十进制”)。http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#DFormatString (2认同)

chr*_*dam 5

您可以尝试使用String.Format

例如,要在数字前添加零,请使用冒号分隔符 :并根据需要写入任意多个零。

String.Format("{0:00000}", 2);          // "00002"
String.Format("{0:D5}", 2);             // "00002"
String.Format("{0:D9}", 6000);          // "000006000"
Run Code Online (Sandbox Code Playgroud)