我怎样才能在formatString中使用百分比%没有它乘以100? [英] How can I use a percent % in FormatString without it multiplying by 100?

查看:1151
本文介绍了我怎样才能在formatString中使用百分比%没有它乘以100?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想格式化整数百分比没有它乘以100,如图这里。因为我的源是一个int,首先除以100是不是有效选项。 ?这是可能的。



  [DisplayFormat(DataFormatString ={0:#%})] 


解决方案

您可以逃脱人物:

  [DisplayFormat(DataFormatString = @{0:#\%})] 






请注意,有使用两种方法 \ 作为转义字符:如果一个前缀字符串和逐字符号( @ ),那么 \ 字符包含在字符串中原来的样子,这意味着作为格式字符串的一部分的 \ 将起作用作为转义字符。



没有 @ 逐字符号, \ s的解释为逃逸字符串的由编译器的,因此需要加以本身转义,如 \\



选择一个或另一个,但不能同时:

  @{ 0:#\%} - >右
{0:#\\%} - >右
@{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:#%}")]

解决方案

You can escape the % character:

[DisplayFormat(DataFormatString = @"{0:#\%}")]


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:#\%}"  -> right
"{0:#\\%}"  -> right
@"{0:#\\%}" -> wrong

这篇关于我怎样才能在formatString中使用百分比%没有它乘以100?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆