string Format - how to change negative sign position

ぐ巨炮叔叔 提交于 2019-12-04 12:17:12
James Manning

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.

Would this work:

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