2

Are there any codes that allow for numerical formatting of data when using string.format?

5 Answers 5

5

Yes, you can specify the formatting after a : in the {} placeholder.

specifier type format output (double 1234.56)
0 zero placeholder {0:00.000} 1234.560
# digit placeholder {0:#.##} 1234.56
. decimal point placeholder {0:0.0} 1234.6
, thousand separator {0:0,0} 1,235
% percentage {0:0%} 123456%

In addition there is the group separator ; this is useful for varying the format, depending on the value of the parameter passed. For example:

string.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);

This will output "£1,240.00" if passed 1243.56. It will output the same format bracketed if the value is negative "(£1,240.00)", and will output the string "Nothing" if the number is zero.

4

Yes, you could format it this way:

string.Format("Format number to: {0 : #.00}", number);
string.Format("Format date to: {0 : MM/dd/yyyy}", date);
2

There are a number. This MS site is probably the best place to look

2

Here is another very good reference that compliments what Keith mentioned.

http://www.scribd.com/doc/2547864/msnetformattingstrings

1

As Keith said above. The most common one I use is currency:

String.Format("{0:c}", 12000);

Which would output £12,000.00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.