SilverLight
SilverLight

Reputation: 20468

string Format - how to change negative sign position

I have a string.Format like this :

string Test = string.Format("{0:#,0}", NegativeNumber);

how can I change the negative sign position (Direction -> left or right)?

Upvotes: 3

Views: 4346

Answers (2)

James Manning
James Manning

Reputation: 13589

The easiest route might be to just have a different format for negative numbers

string Test = string.Format("{0:#,0;#,0-}", NegativeNumber);

Results:

PS C:\> '{0:#,0;#,0-}' -f -17.2

17-

PS C:\> '{0:#,0;#,0-}' -f 17.2

17

Custom Numeric Format Strings

The semicolon (;) is a conditional format specifier that applies different formatting to a number depending on whether its value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons. These sections are described in the following table.

Upvotes: 8

SiggyF
SiggyF

Reputation: 23195

Would this work:

String.Format("{0:0.00;0.00-;zero}", -123.4567); 

Upvotes: 0

Related Questions