asp.net string and string.formt number

Source: Internet
Author: User
Tags numeric local time month name

C # formatted numeric results table
Character
Description
Example
Output

C Currency String.Format ("{0:c3}", 2) $2.000
D decimal String.Format ("{0:d3}", 2) 002
E Scientific counting method 1.20e+001 1.20e+001
G General String.Format ("{0:g}", 2) 2
n Numeric String.Format separated by semicolons ("{0:n}", 250000) 250,000.00
x hexadecimal String.Format ("{0:x000}", "C")


String.Format ("{0:000.000}", 12.2) 012.200

Strings
There really isn ' t any formatting within a strong, beyond it ' s alignment. Alignment works for any argument being printed in a String.Format call.

Sample generates
String.Format ("->{1,10}<-", "Hello"); -> hello<-
String.Format ("->{1,-10}<-", "Hello"); ->hello <-


Numbers
Basic number formatting specifiers:

Specifier type format output
(Passed
Double 1.42)
Output
(Passed
int-12400)

C Currency {0:c} $1.42-$12,400
d Decimal (whole number) {0:d} system.
formatexception-12400
E Scientific {0:e} 1.420000e+000-1.240000e+004
F fixed point {0:f} 1.42-12400.00
G General {0:g} 1.42-12400
N number with commas for thousands {0:n} 1.42-12,400
R round trippable {0:r} 1.42 system.
FormatException
x hexadecimal {0:x4} system.
FormatException cf90


Custom number formatting:

Specifier Type example output (passed double 1500.42) note
0 Zero placeholder {0:00.0000} 1500.4200 pads with zeroes.
# digit Placeholder {0: (#). # #} (1500). 42
. decimal point {0:0.0} 1500.4
, thousand separator {0:0,0} 1,500 must be between two.
,.  Number Scaling {0:0,.} 2 comma adjacent to period scales by 1000.
% percent {0:0%} 150042% multiplies by, adds% sign.
e exponent placeholder {0:00e+0} 15e+2 many exponent formats.
; Group Separator-Below


The group separator is especially useful for formatting currency values which require that negative values are enclosed in Parentheses. This currency formatting example in the bottom of this document makes it obvious:

Dates
Note This date formatting is especially dependant on the system ' s regional settings; The example strings here is are from my local locale.

Specifier Type example (passed System.DateTime.Now)
D Short Date 10/12/2002
D Long Date December 10, 2002
T short time 10:11 PM
T long time 10:11:29 PM
F Full Date & Time December, 2002 10:11 pm
F Full Date & Time (Long) December, 2002 10:11:29 pm
G Default Date & Time 10/12/2002 10:11 pm
G Default Date & Time (long) 10/12/2002 10:11:29 pm
M month Day pattern December 10
R rfc1123 Date string Tue, Dec 2002 22:11:29 GMT
s sortable date string 2002-12-10t22:11:29
U Universal sortable, local time 2002-12-10 22:13:50z
U universal sortable, GMT December, 2002 3:13:50 am
Y Year Month pattern December, 2002


The ' u ' specifier seems broken; That string certainly isn ' t sortable.

Custom date formatting:

Specifier Type Example Example output
DD Day {0:DD} 10
DDD Day name {0:DDD} Tue
dddd Full day name {0:DDDD} Tuesday
F, FF, ... second fractions {0:fff} 932
GG, ... era {0:GG} A.D.
HH 2 digit Hour {0:HH} 10
HH 2 digit Hour, 24hr format {0:HH} 22
MM minute 00-59 {0:mm} 38
MM month 01-12 {0:mm} 12
Mmm month abbreviation {0:mmm} Dec
Mmmm full month name {0:mmmm} December
SS seconds 00-59 {0:SS} 46
tt am or pm {0:tt} PM
YY year, 2 digits {0:YY} 02
YYYY year {0:yyyy} 2002
ZZ TimeZone Offset, 2 digits {0:zz}-05
ZZZ full timezone offset {0:zzz} -05:00
: separator {0:HH:MM:SS} 10:43:20
/separator {0:DD/MM/YYYY} 10/12/2002


Enumerations

Specifier type
G Default (flag names if available, otherwise decimal)
F Flags always
D Integer always
x eight digit hex.


Some useful examples
String.Format ("{0:$#,# #0; ($#,# #0); zero}", value);

This would output "$1,240.00" if passed 1243.50. It would output the same format but in parentheses if the # is negative, and would output the string "zero" if the Numb ER is zero.

String.Format ("{0: (###) ###-####}", 18005551212);

This is output "(800) 555-1212".

Variables. ToString ()


Character conversion to string
12345.tostring ("n"); Generate 12,345.00
12345.tostring ("C"); Generate ¥12,345.00
12345.tostring ("E"); Generate 1.234500e+004
12345.tostring ("F4"); Generate 12345.0000
12345.tostring ("X"); Generate 3039 (16 binary)
12345.tostring ("P"); Generate 1,234,500.00%


protected void Button1_Click (object sender, EventArgs e)
{
String S1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
Double num = 2.5;
S1 = String.Format ("CC {0:C}", num); cc¥2.50
S2 = String.Format ("dd {0:d}", (int) num); DD 2
S3 = String.Format ("e {0:e}", num); E 2.500000e+000
S4 = String.Format ("e {0:e}", num); E 2.500000e+000
S5 = String.Format ("FF {0:f}", num); FF 2.50
S6 = String.Format ("GG {0:g}", num); GG 2.5
S7 = String.Format ("NN {0:n}", num); NN 2.50
S8 = String.Format ("pp {0:p}", num); PP 250%
S9 = String.Format ("RR {0:r}", num); RR 2.5
S0 = String.Format ("xx {0:x}", (int) num); XX 2

string br = "n";
TextBox1.Text = String.Concat (S1, BR, S2, BR, S3, BR, S4, BR, S5, BR, S6, BR, S7, BR, S8, BR, S9, BR, S0);
}

--------------------------------------------------------------------------------

Accuracy of standard numeric formats:
--------------------------------------------------------------------------------

protected void Button1_Click (object sender, EventArgs e)
{
String S1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
Double num = 2.5;
S1 = String.Format ("CC {0:C3}", num); cc¥2.500
S2 = String.Format ("dd {0:d3}", (int) num); DD 002
S3 = String.Format ("e {0:e3}", num); E 2.500e+000
S4 = String.Format ("e {0:e3}", num); E 2.500e+000
S5 = String.Format ("FF {0:F3}", num); FF 2.500
S6 = String.Format ("GG {0:g3}", num); GG 2.5
S7 = String.Format ("NN {0:n3}", num); NN 2.500
S8 = String.Format ("pp {0:p3}", num); PP 250%
S9 = String.Format ("RR {0:R3}", num); RR 2.5
S0 = String.Format ("xx {0:x3}", (int) num); XX 002

string br = "n";
TextBox1.Text = String.Concat (S1, BR, S2, BR, S3, BR, S4, BR, S5, BR, S6, BR, S7, BR, S8, BR, S9, BR, S0);
}

--------------------------------------------------------------------------------

Custom number formats:
--------------------------------------------------------------------------------

protected void Button1_Click (object sender, EventArgs e)
{
String S1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
Double num =-1234.567;
S1 = String.Format ("{0:000000.00}", num); -001234.57
S2 = String.Format ("{0:######.##}", num); -1234.57
S3 = String.Format ("{0:#,#.####}", num); -1,234.567
S4 = String.Format ("{0:0,0.0000}", num); -1,234.5670
S5 = String.Format ("{0:######.000000}", num); -1234.567000
S6 = String.Format ("{0:000000.######}", num); -001234.567
S7 = String.Format ("{0:#.00%}", num); -123456.7%
S8 = String.Format ("{0:%#.00}", num); -%123456.70
S9 = String.Format ("{0: (+|0) #.#;(-) #.#}", num); (-) 1234.6
S0 = String.Format ("{0: (+) #.#;(-) #.#;(0)}", num); (-) 1234.6

    String br = "n";
    TextBox1.Text = String.Concat (S1, BR, S2, BR, S3, BR, S4, BR, S5, BR, S6, BR, S7, BR, S8, BR, S9, BR, S0);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.