C#:Formatting

From Progzoo
Jump to: navigation, search

You can format numbers using the String.format method. See C#:Format Printing for details of some other options that are available.

You can build a string using String.Format. The {0}, {1}, ... values are substituted with the parameters that follow the format string

Command                           Result
String.Format( {0} {1}",100,200)  100 200
String.Format("{0:n0}.",12345)     12,345
String.Format("{0:n2}.",12345);    12,345.00
String.Format("x " {0,8}.",12345);    12345

String.Format("{0:n2}.",12345); The output with :n0 includes commas and zero decimal places:

12,345


[Font] [Default] [Show] [Resize] [History] [Profile]