Is there a way to force showing the +
sign in front of positive numbers when using StringFormat
?
使用StringFormat时,是否有办法强制在正数前面显示+号?
For example:
例如:
<TextBlock Text="{Binding Path=PercentAgainstBudget,
StringFormat={}{0:0.00}%}" />
If PercentAgainstBudget
is negative I see the -
sign. But if its positive, it does not. Since this number is an offset, I'd like to force showing +/-
. I could make a ValueConverter
but I'm wondering if there's a way to do it through the StringFormat
property.
如果百分之一的预算是负数,我看到了。但如果它是正数,它就不是。由于这个数字是一个偏移量,我想强制显示+/-。我可以做一个ValueConverter,但是我想知道是否可以通过StringFormat属性来实现它。
1 个解决方案
#1
23
The format string can be made of two parts separated by a semicolon. First part is the format of positive numbers, second of negative. You'll want this: +0.0%;-0.0%
格式字符串可以由分号分隔的两部分组成。第一部分是正数的格式,第二部分是负数的格式。你需要的是这样的:+ 0.0%;-0.0%
PS C:\Users\jachymko> '{0:+0.0%;-0.0%}' -f 2.45
+245,0%
PS C:\Users\jachymko> '{0:+0.0%;-0.0%}' -f -2.45
-245,0%
#1
23
The format string can be made of two parts separated by a semicolon. First part is the format of positive numbers, second of negative. You'll want this: +0.0%;-0.0%
格式字符串可以由分号分隔的两部分组成。第一部分是正数的格式,第二部分是负数的格式。你需要的是这样的:+ 0.0%;-0.0%
PS C:\Users\jachymko> '{0:+0.0%;-0.0%}' -f 2.45
+245,0%
PS C:\Users\jachymko> '{0:+0.0%;-0.0%}' -f -2.45
-245,0%