c# - How to format a string as a number

String format number
string-format-number.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
  
<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        int intValue = 10;
        int intValue2 = 123456;
        int intValue3 = 40;
        int intValue4 = 5421;
        int intValue5 = 5;

        Label1.Text = "formatted string..............";
        Label1.Text += "<br /> Number: " + intValue + " [after string format] " + intValue.ToString("D5");
        Label1.Text += "<br /> Number: " + intValue2 + " [after string format] " + intValue2.ToString("##-##-##");
        Label1.Text += "<br /> Number: " + intValue3 + " [after string format] " + intValue3.ToString("0.0");
        Label1.Text += "<br /> Number: " + intValue4 + " [after string format] " + intValue4.ToString("0,0.00");
        Label1.Text += "<br /> Number: " + intValue5 + " [after string format] " + intValue5.ToString("00.00");
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string format number</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string format number
        </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"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples