c# - How to format a decimal string as percentage value

String format decimal percentage
string-format-decimal-percentage.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this line create a decimal variable.
        Decimal decimalValue1 = 0.5567M;

        Label1.Text = "decimal value1: " + decimalValue1.ToString();

        //string format decimal to int using string.format method.
        Label1.Text += "<br /><br />decimal value1 percent format: " + string.Format("{0:p}", decimalValue1);
        Label1.Text += "<br />percent Format with 0 decimal digits: " + string.Format("{0:p0}", decimalValue1);
        Label1.Text += "<br />percent Format with 1 decimal digits: " + string.Format("{0:p1}", decimalValue1);
        Label1.Text += "<br />percent Format with 2 decimal digits: " + string.Format("{0:p2}", decimalValue1);
        Label1.Text += "<br />percent Format with 3 decimal digits: " + string.Format("{0:p3}", decimalValue1);

        Label1.Text += "<br /><br />using ToString method: " + decimalValue1.ToString("p1");
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string format decimal percentage</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string format decimal percentage
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string format decimal percentage"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples