string Format - how to change negative sign positi

2020-07-22 17:14发布

问题:

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)?

回答1:

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.



回答2:

Would this work:

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


标签: c# string sign