String format number with sign
string-format-number-with-sign.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this line create an int variable.
int number = 12345;
Label1.Text = "formatted string..............";
int length = number.ToString().Length + 1;
int length2 = length + 1;
Label1.Text += "<br />Number: " + number;
//format string using ToString method
Label1.Text += "<br />number padding with plus(+) sign: " + number.ToString("+#");
//format string using string.format method.
Label1.Text += "<br />number padding with plus sign: " + string.Format("+{0}", number);
//another technique to format string using padleft method.
Label1.Text += "<br /><br />number padding with '#' sign double pad: " + number.ToString().PadLeft(length2, '#');
Label1.Text += "<br /><br />number padding with '@' sign: " + number.ToString().PadLeft(length, '@');
Label1.Text += "<br /><br />number padding with dollar($) sign: " + number.ToString().PadLeft(length, '$');
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string format number with sign</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string format number with sign
</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 with sign"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to format a string as a fixed length number
- String format number two digits
- How to format a string as currency
- How to replace a character in a string
- How to perform string replace by ignoring case
- How to escape characters within a string
- How to get the first character of a string
- How to remove characters from a string
- How to trim a string to a specified length
- How to convert a string to a datetime object