Custom numeric format string to always display the sign
Solution 1
Yes, you can. There is conditional formatting. See Conditional formatting in MSDN
eg:
string MyString = number.ToString("+0;-#");
Where each section separated by a semicolon represents positive and negative numbers
or:
string MyString = number.ToString("+#;-#;0");
if you don't want the zero to have a plus sign.
Solution 2
Beware, when using conditional formatting the negative value doesn't automatically get a sign. You need to do
string MyString = number.ToString("+#;-#;0");
Solution 3
You can also use format strings in string.Format(); the format string is separated from the index with a colon (':')
var f = string.Format("{0}, Force sign {0:+#;-#;+0}, No sign for zero {0:+#;-#;0}", number);
For number { +1, -1, 0 } this gives:
1, Force sign +1, No sign for zero +1
-1, Force sign -1, No sign for zero -1
0, Force sign +0, No sign for zero 0
You can also use an interpolated string instead of string.Format
to obtain the same result:
var f = $"{number}, Force sign {number:+#;-#;+0}, No sign for zero {number:+#;-#;0}";
Solution 4
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:
String.Format("{0:+#;-#;+0}", 0)); // output: +0
or
String.Format("{0:+0;-#}", 0)); // output: +0
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)
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.
Also please keep in mind that if you need any decimal precision you need to specify it like that:
String.Format("{0:+0.##;-#.##}", 0)); // output: +0
or, if you wan't zeros to always show up, like that:
String.Format("{0:+0.00;-#.00}", 0)); // output: +0.00
Solution 5
For a numeric expression of any type:
+###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;-###,###,###,###,###,###,###,###,###,##0.###,###,###,###,###,###,###,###,###,###;0
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).
Related videos on Youtube
Tushee
Full-stack Software Architect and Developer in C#, .NET, Javascript, Typescript, AngularJS, Angular .NET, JADE, related web technologies. SOreadytohelp
Updated on July 08, 2022Comments
-
Tushee almost 2 years
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!)
-
Roman Starkov over 12 yearsFixed a bug whereby negatives did not get a minus.
-
rbaleksandar over 11 yearsThanks you so much! I have been busting my head for like an hour to figure out what's the equivalent of the '+'-flag of printf in C# and you gave me THE answer.
-
orion elenzil about 10 yearswith
+#;-#
, for the value 0 I get just "+", with no actual 0. for me I want 0 to appear as "+0", so this works:+#;-#;+0
. -
Shahab almost 9 yearsThanks! Mixing this with
''
will do great things! Look at this:Changes.ToString("'('#' ▲)';'('#' ▼)';''")
! It saved me lots of time and hundreds ofif
statements! :) -
Kamil Szot over 8 yearsLast line of your answer should say "Default format 0, Force sign +, No sign for zero 0." because that's the actual result of
+#;-#
format for value 0 -
Kamil Szot over 8 years@orionelenzil I had the same problem. You could also do
+0;-#
instead of+#;-#;+0
This answer was really misleading for me. -
Edward over 8 yearsThank you, Kamil - I missed that. I have now corrected the force sign format: +#;-#;+0 (instead of +#;-#)
-
Andrew about 8 yearsAs others pointed out,
+0;-#
gives you the same result with cleaner code. -
Andrew about 8 yearsYou can get the force sign behavior with a simpler format:
+0;-#
. :) -
Edward over 5 years@Andrew, Indeed, but I prefer
+#;-#;+0
over+#;-#
as this makes it clearer that 0 will be formatted as +0 -
Glenn Slayden about 3 years@Shahab I like it. Note that you don't even have to display the numeric value of the number with the controlling sign if you don't want to...
string msg = $"The stock market is {delta:▲;▼;~} today.";
. -
navigator almost 2 yearsA format of
#,##0
is sufficient for any number of thousands separators.