Brains Engineering - Il Blog

Formattare le stringhe in .Net

scritto da giox 02/05/2008 15.07.00

Uno dei metodi di formattazione più utili ed ampiamente utilizzabili è String.Format ().
Si può usare per formattare numeri, sequenze, date e tempi, restituendo una sequenza di caratteri.
La sintassi generale per questo è mostrata sotto.

String.Format("{index:format} [{index:format}]...", value [,value]...)

Formattare i numeri

FormatDescription
Cn|cn Currency. The number is converted to a string using the optional n (precision specifier) to indicate the number of decimal places to which to round and display. The number is preceded by a dollar sign with commas separating thousands positions. Negative values are enclosed in parentheses.
Dn|dn Decimal. The integer (only) number is converted to a string using the optional n (precision specifier) to indicate the number of decimal positions to which to round and display, padded with leading 0s if necessary. Negative values are preceded by a minus sign.
Fn|fn Fixed-point. The number is converted to a string using the optional n (precision specifier) to indicate the number of decimal positions to the right of the decimal point to which to round. Negative values are preceded by a minus sign. No comma separators are supplied.
Nn|nn Number. The number is converted to a string using the optional n (precision specifier) to indicate the number of decimal positions to the right of the decimal point to which to round. Negative values are preceded by a minus sign. Comma separators are supplied for the thousands positions.
Pn|pn Percent. The number is multiplied by 100 and converted to a string using the optional n (precision specifier) to indicate the number of decimal positions to the right of the decimal point to which to round. Negative values are preceded by a minus sign.

FormatExampleOutput
Cn|cn String.Format("{0:C}", 1234.56) $1,234.56
String.Format("{0:C3}", 1234.56789) $1,234.568
String.Format("{0:C}", -1234.5) ($1,234.50)
Dn|dn String.Format("{0:D}", 1234) 1234
String.Format("{0:D6}", 1234) 001234
String.Format("{0:D}", 11234) -1234
Fn|fn String.Format("{0:F}", 1234.56) 1234.56
String.Format("{0:F4}", 1234.56) 1234.5600
String.Format("{0:F1}", -1234.567) -1234.6
Nn|nn String.Format("{0:N}", 1234.56) 1,234.56
String.Format("{0:N4}", 1234.56) 1,234.5600
String.Format("{0:N1}", -1234.567) -1,234.6
Pn|pn String.Format("{0:P}", .1234) 12.34 %
String.Format("{0:P0}", .1234) 12 %
String.Format("{0:P1}", -.1234) -12.3 %
 
FormatDescription
# Digit placeholder. A digit appearing in the number at this position also appears in the output string at this position; otherwise, no digit appears. A single # represents as many significant digits as appear in the number. Leading 0s are never displayed at the position. Negative numbers are displayed with a minus sign.
0 Zero placeholder. A digit appearing in the number at this position also appears in the output string at this position; otherwise a 0 appears. A single 0 represents as many significant digits as appear in the number. Additional 0 characters beyond the number of significant digits in the number represent 0 padding. Negative numbers are displayed with a minus sign.
. Decimal point. The location of the decimal point in the formatted number. # and 0 characters indicate the number of positions to the right of the decimal point. The # character position is filled with significant digits; the 0 characters pad with zeros. Numbers are rounded to the last indicated decimal position.
, Thousand separator. Indicates inclusion of commas as thousands separators in the output format. Must appear between two or more # or 0 digit indicators to the left of a decimal point.
% Percent placeholder. The number is multiplied by 100 prior to formatting. The % sign appears to the right of the displayed number.
Other Literal characters. All other characters appearing in the format string are represented as literal characters in the indicated position in the output string.
 
FormatExampleOutput
# String.Format("{0:#}", 12345) 12345
String.Format("{0:#}", -12345) -12345
String.Format("{0:#######}", 12345) 12345
0 String.Format("{0:0}", 12345) 12345
String.Format("{0:0}", -12345) -12345
String.Format("{0:0#####}", 12345) 012345
. String.Format("{0:#.##}", 123.45) 123.45
String.Format("{0:#.000}", 123.45) 123.450
String.Format("{0:#.##}", -123.456) -123.46
, String.Format("{0:#,#.##}", 1234.56) 1,234.56
String.Format("{0:#,#.#0}", 1234.5) 1,234.50
String.Format("{0:#,#.#0}", -1234.567) -1,234.57
% String.Format("{0:0.##%}", .1234) 12.34%
String.Format("{0:0.00%}", .12) 12.00%
String.Format("{0:0.00%}", .12345) 12.35%
Other String.Format("{0:$#,#.##}", 1234.56) $1,234.56
String.Format("{0:$ #,#.##}", 1234.56) $ 1,234.56
String.Format("{0:$#,###.##}", 1234.56) $1,234.56
String.Format("{0:[#,#.##]}", 1234.56) [1,234.56]
String.Format("{0:#0 Percent}", 12) 12 Percent
String.Format("You owe: {0:$#,#.##}", 1234.56) You owe: $1,234.56
 

Formattare le date

FormatDescription
d Short date. Displays the short date format.
D Long date. Displays the long date format.
t Short time. Displays the short time format.
T Long time. Displays the long time format.
f Full date/short time. Displays the full date/time format with short time.
F Full date/long time. Displays the full date/time format with long time.
g General date/short time. Displays the full date/time format with short time.
G General date/long time. Displays the full date/time format with long time.
M|m Month/day. Displays the month name and day format.
Y|y Month/year. Displays the month name and year format.
s Sortable date/time. Displays the sortable date/time format.

 

FormatExampleOutput
d String.Format("{0:d}", Now) 5/2/2008
D String.Format("{0:D}", Now) Friday, May 02, 2008
t String.Format("{0:t}", Now) 3:57 AM
T String.Format("{0:T}", Now) 3:57:03 AM
f String.Format("{0:f}", Now) Friday, May 02, 2008 3:57 AM
F String.Format("{0:F}", Now) Friday, May 02, 2008 3:57:03 AM
g String.Format("{0:g}", Now) 5/2/2008 3:57 AM
G String.Format("{0:G}", Now) 5/2/2008 3:57:03 AM
M String.Format("{0:M}", Now) May 02
Y String.Format("{0:Y}", Now) May, 2008
s String.Format("{0:s}", Now) 2008-05-02T03:57:03

 

FormatDescription
d Displays the day of the month as a single or double digit. (If this character appears without additional formatting it is interpreted as the standard short date format.)
dd Displays the day of the month with a leading 0 for single-digit months.
dddd Displays the full name of the day.
M Displays the numeric month as a single or double digit. (If this character appears without additional formatting it is interpreted as the standard month/day format.)
MM Displays the numeric month with a leading 0 for single-digit months.
MMM Displays the abbreviated name of the month.
MMMM Displays the full name of the month.
y Displays the year as a one- or two-digit number with the century digits omitted. (If this character appears without additional formatting it is interpreted as the standard month/year format.)
yy Displays the year as a two-digit number with a leading 0 for single-digit years, with the century digits omitted.
yyyy Displays the year as a four-digit number.
H|h Displays the hour as a one- or two-digit number. (Cannot appear stand-alone; must appear with other time format characters.)
m Displays the minute as a one- or two-digit number. (If it appears stand-alone it is interpreted as the standard month/day format.)
mm Displays the minute as a two-digit number with a leading 0 for single-digit minutes.
s Displays the seconds as a one- or two-digit number. (If it appears stand-alone it is interpreted as the standard sortable date/time format.)
ss Displays the seconds as a two-digit number with a leading 0 for single-digit seconds.
f... Displays fractions of seconds using up to 7 characters to display fractional digits.
/ - , Date separators.
: Time separator.
Other All other characters in the date/time format string are written to the output string as literal characters.

 

FormatExampleOutput
d String.Format("{0:d}", Now) 5/2/2008
dd String.Format("{0:dd}", Now) 02
dddd String.Format("{0:dddd}", Now) Friday
M String.Format("{0:M}", Now) May 02
MM String.Format("{0:MM}", Now) 05
MMM String.Format("{0:MMM}", Now) May
String.Format("{0:MMM dd}", Now) May 02
String.Format("{0:dd MMM}", Now) 02 May
MMMM String.Format("{0:MMMM}", Now) May
String.Format("{0:MMMM dd}", Now) May 02
String.Format("{0:dd MMMM}", Now) 02 May
y String.Format("{0:y}", Now) May, 2008
String.Format("{0:M d y}", Now) 5 2 8
yy String.Format("{0:yy}", Now) 08
String.Format("{0:M d yy}", Now) 5 2 08
yyyy String.Format("{0:yyyy}", Now) 2008
String.Format("{0:d MMMM yyyy}", Now) 2 May 2008
/ - , String.Format("{0:dd/MM/yyyy}", Now) 02/05/2008
String.Format("{0:dd-MMMM-yyyy}", Now) 02-May-2008
String.Format("{0:MMMM d, yyyy}", Now) May 2, 2008
H String.Format("{0:HH}", Now) 03
mm String.Format("{0:mm}", Now) 57
String.Format("{0:H:mm}", Now) 3:57
ss String.Format("{0:ss}", Now) 03
String.Format("{0:H:m:ss}", Now) 3:57:03
f... String.Format("{0:H:mm:ssffff}", Now) 3:57:0314855
Other String.Format("Today is {0:MMM d, yyyy}", Now) Today is May 2, 2008
String.Format("The time is {0:H:mm:ss}", Now) The time is 3:57:03

 

 

Correntemente valutato 3.0 da 2 utenti

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Categorie: .NET

Post correlati

Commenti

12/07/2011 19.23.30

thesis writing

bella amico

thesis writing us

03/09/2011 18.27.24

tragamonedas

grazie per di metodi di formattazione più utili ed ampiamente utilizzabili è String

tragamonedas gb

Aggiungi commento


(Visualizza la tua icona Gravatar)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Anteprima

20/05/2012 1.48.01

Cerca



Calendario

<<  maggio 2012  >>
lumamegivesado
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

© Copyright 2012

Sign in