string Format - how to change negative sign position

混江龙づ霸主 提交于 2019-12-06 07:26:23

问题


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


来源:https://stackoverflow.com/questions/3287372/string-format-how-to-change-negative-sign-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!