C# 如何将值格式化为没有百分号的百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2075835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I format a value as a percentage without the percent sign?
提问by AngryHacker
float f = 0.479f;
Console.WriteLine(f.ToString("p1"));
The output: 47.9 %
产量:47.9%
What should I pass to ToString() in order to remove the percentage sign for output like this:
我应该将什么传递给 ToString() 以删除输出的百分比符号,如下所示:
47.9
47.9
EDIT. I should have mentioned that I am passing the mask to a 3rd party component that does it's thing with it. I can't perform any acrobatics with the numbers unfortunately. It has to be the mask that does the trick.
编辑。我应该提到我正在将掩码传递给与它相关的第 3 方组件。不幸的是,我不能用数字表演任何杂技。必须是面具才能发挥作用。
采纳答案by Dynami Le Savard
I should have mentioned that I am passing the mask to a 3rd party component that does it's thing with it. I can't perform any acrobatics with the numbers unfortunately. It has to be the mask that does the trick.
我应该提到我正在将掩码传递给与它相关的第 3 方组件。不幸的是,我不能用数字表演任何杂技。必须是面具才能发挥作用。
So I assume that you are stuck with Float.ToString(String)
, and that you cant only edit the p1
in f.ToString("p1")
. Now that's a tricky one. If you're not afraid of breaking anything related to the changes implied, you could do the following:
所以我假设你被困在了Float.ToString(String)
,并且你不能只编辑p1
in f.ToString("p1")
。现在这是一个棘手的问题。如果您不怕破坏与隐含更改相关的任何内容,则可以执行以下操作:
The "P" numeric format, as documented On MSDN, uses NumericFormatInfo.PercentSymbol
in order to write the "%" sign. NumericFormatInfo
is a member of your current CultureInfo
. What you could do, is clone your CurrentCulture
, and modify the PercentSymbol to ""
like the following:
在“P”的数字格式,如记录在MSDN,用途NumericFormatInfo.PercentSymbol
,以写“%”的标志。NumericFormatInfo
是您当前CultureInfo
. 您可以做的是克隆您的CurrentCulture
,然后将 PercentSymbol 修改""
为如下所示:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
float f = 0.479f;
Console.WriteLine(f.ToString("p1")); //will write 47.9%
CultureInfo ci = CultureInfo.CurrentCulture.Clone() as CultureInfo;
ci.NumberFormat.PercentSymbol = "";
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
Console.WriteLine(f.ToString("p1")); //will write 47.9
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
This would be the way to go if you don't want to alter the ToString call.
如果您不想更改 ToString 调用,这将是要走的路。
回答by keyboardP
Could try
可以试试
Console.WriteLine(f.ToString("p1").Replace('%',' '));
Or, if you want don't want a space, use Replace('%','\0')
或者,如果您不想使用空格,请使用 Replace('%','\0')
EDIT: If you can only use the ToString()
method, you could create a NumberFormatInfo
object, set the percentage symbol to be blank and pass it into the method.
(NumberFormatInfo
is in the System.Globalization
namespace)
e.g.
编辑:如果您只能使用该ToString()
方法,则可以创建一个NumberFormatInfo
对象,将百分比符号设置为空白并将其传递给该方法。(NumberFormatInfo
在System.Globalization
命名空间中)例如
NumberFormatInfo myNewFormat = new NumberFormatInfo();
myNewFormat.PercentSymbol = "";
float f = 0.479f;
Console.WriteLine(f.ToString("p1",myNewFormat));
回答by Rubens Farias
Console.WriteLine((f * 100).ToString("0.0"));
回答by Noldorin
How about just the following?
仅以下内容如何?
Console.WriteLine((f * 100).ToString("F1"));
e.g. f = 0.4792 --> "47.9"
例如 f = 0.4792 --> "47.9"
A list and descriptions of standard numeric format are available here on MSDN, in case it helps.
MSDN上提供了标准数字格式的列表和说明,以防万一。