String Format - how to change the position of a negative sign

I have string.Format:

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

How can I change the position of a negative sign (Direction → left or right)?

+3
source share
2 answers

The easiest way is to simply 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 Number Format Strings

(;) - , , , . , , . .

+5

:

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

Source: https://habr.com/ru/post/1755447/


All Articles