如何在 FormatString 中使用百分比 % 而不将其乘以 100?
我想将整数格式化为百分比,而不将其乘以 100,如下所示 此处。因为我的源是一个 int,所以先将其除以 100 不是一个有效的选项。这可能吗?
[DisplayFormat(DataFormatString = "{0:#%}")]
I would like to format an integer as a percent without it multiplying by 100 as shown here. Because my source is an int, dividing it first by 100 is not a valid option. Is this possible?
[DisplayFormat(DataFormatString = "{0:#%}")]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以转义
%
字符:请注意,有两种方法可以使用
\
作为转义字符:如果您使用逐字符号 (@
),那么\
字符将按原样包含在字符串中,这意味着作为格式字符串的一部分,单个\
将用作转义字符。如果没有
@
逐字符号,\
会被编译器解释为转义字符串 ,因此需要自行转义,如\\。
选择其中之一,但不能同时选择两者:
You can escape the
%
character:Note that there are two ways to use
\
as an escape character: if you prefix a string literal with the verbatim symbol (@
), then\
characters are included in the string as-is, which means that as part of a format string a single\
will function as an escape character.Without the
@
verbatim symbol,\
s are interpreted as escape strings by the compiler and as such need to be escaped themselves, as\\
.Pick one or the other, but not both:
将 % 放在 {0:..} 之外
Put the % outside the {0:..}
从您的链接页面:
\
转义字符
导致下一个字符被解释为文字而不是自定义格式说明符。
From your linked page:
\
Escape character
Causes the next character to be interpreted as a literal rather than as a custom format specifier.
如果值为 0,则格式 @"{0:#%}" 仅显示 %。它应该显示 0%。所以正确的格式是
哈希后有一个额外的零。
If the value is 0, the format @"{0:#%}" displays just a %. It should display 0%. So the correct format is
There is an extra zero after the hash.