String format number leading zero
string-format-number-leading-zero.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this line create a int variable.
int number = 12345;
Label1.Text = "formatted string..............";
int length = number.ToString().Length + 1;
string dValue = "D" + length.ToString();
Label1.Text += "<br />Number: " + number;
//format string using ToString method
Label1.Text += "<br />String format number leading zero: " + number.ToString("D6");
//format string using string.format method.
Label1.Text += "<br />String format number leading zero: " + string.Format("{0:D6}",number);
//dynamically determine number length and format string.
Label1.Text += "<br /><br />Another dynamic option: " + number.ToString(dValue);
//another technique to format string using padleft method.
Label1.Text += "<br /><br />Another technique: " + number.ToString().PadLeft(length,'0');
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string format number leading zero</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string format number leading zero
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="string format number leading zero"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to convert a string to int
- How to get a substring from a string
- How to compare two strings by ignoring case
- How to format a string as a phone number
- How to format a string as a number with 2 decimal places
- How to format a string as a number with padding
- How to format a decimal string to currency
- How to format a decimal string to int
- How to check whether a string contains numbers only
- String contains list of strings