我想要的是这样的:
String.Format("Value: {0:%%}.", 0.8526)
其中%%是格式提供程序或我要查找的任何内容。结果应该是:Value: %85.26.
。
我基本上需要它来进行wpf绑定,但首先让我们解决一般的格式化问题:
<TextBlock Text="{Binding Percent, StringFormat=%%}" />
发布于 2009-11-24 23:56:01
使用P
format string。这将随文化的不同而不同:
String.Format("Value: {0:P2}.", 0.8526) // formats as 85.26 % (varies by culture)
发布于 2015-06-09 04:20:19
如果您有充分的理由搁置与区域性相关的格式设置,并显式控制值和"%“之间是否存在空格,以及"%”是前导还是尾随,则可以使用NumberFormatInfo的PercentPositivePattern和PercentNegativePattern属性。
例如,要获取一个尾随"%“且值与”%“之间没有空格的十进制值:
myValue.ToString("P2", new NumberFormatInfo { PercentPositivePattern = 1, PercentNegativePattern = 1 });
更完整的示例:
using System.Globalization;
...
decimal myValue = -0.123m;
NumberFormatInfo percentageFormat = new NumberFormatInfo { PercentPositivePattern = 1, PercentNegativePattern = 1 };
string formattedValue = myValue.ToString("P2", percentageFormat); // "-12.30%" (in en-us)
发布于 2018-07-28 06:53:10
如果您希望使用一种允许您像输入一样保留数字的格式,则此格式适用于我:"# \\%"
https://stackoverflow.com/questions/1790975
复制相似问题