Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it should do for zero, I'm not sure!)
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
You can also use format strings in string.Format(); the format string is separated from the index with a colon (':')
For number { +1, -1, 0 } this gives:
You can also use an interpolated string instead of
string.Format
to obtain the same result:For a numeric expression of any type:
Use three parts for three cases: positive;negative;zero
Other aspects of the example:
Zero is not signed. You could have it show as anything, such as "zero".
Absolute values less than one have a leading 0 before the decimal point. Adjust to taste.
Number of digits is for the largest and smallest absolute decimal values. Adjust to taste.
Decimal point character is culture-specific. .NET substitutes.
Grouping separators are optional. The character is culture-specific. .NET substitutes. (The positions are also culture-specific but that's only controlled by your format string.) You also use any other character except the special characters for Format (which include , . # 0).
Contrary to the other answers it seems that if you want to get +1, -1, +0 (for arguments 1, -1, 0) you need to use the format:
or
If you use just
+#;-#
it will display just+
(not+0
) for 0.The "#" Custom Specifier (at https://msdn.microsoft.com/en-us/library/0c899ak8.aspx)
Also please keep in mind that if you need any decimal precision you need to specify it like that:
or, if you wan't zeros to always show up, like that:
Yes, you can. There is conditional formatting. See Conditional formatting in MSDN
eg:
Where each section separated by a semicolon represents positive and negative numbers
or:
if you don't want the zero to have a plus sign.
Beware, when using conditional formatting the negative value doesn't automatically get a sign. You need to do