如何将值格式化为不带百分号的百分比?

发布于 2024-08-17 20:52:49 字数 252 浏览 7 评论 0原文

float f = 0.479f;
Console.WriteLine(f.ToString("p1"));

输出: 47.9 %

我应该将什么传递给 ToString() 以删除输出的百分号,如下所示:

47.9

编辑。我应该提到我正在将掩码传递给第三方组件来处理它。不幸的是,我无法用这些数字表演任何杂技。一定是面具发挥了作用。

float f = 0.479f;
Console.WriteLine(f.ToString("p1"));

The output: 47.9 %

What should I pass to ToString() in order to remove the percentage sign for output like this:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无远思近则忧 2024-08-24 20:52:49

我应该提到我是
将面具传递给第三方
做它的事情的组件
它。我不会表演任何杂技
不幸的是,数字。它有
成为发挥作用的面具。

所以我假设您被 Float.ToString(String) 困住了,并且您不能只编辑 f.ToString("p1")< 中的 p1 /代码>。现在这是一个棘手的问题。如果您不担心破坏与隐含更改相关的任何内容,则可以执行以下操作:

“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());

        }
    }

如果您不想更改,这将是可行的方法ToString 调用。

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.

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:

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:

    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.

夜吻♂芭芘 2024-08-24 20:52:49
Console.WriteLine((f * 100).ToString("0.0"));
Console.WriteLine((f * 100).ToString("0.0"));
氛圍 2024-08-24 20:52:49

仅以下内容怎么样?

Console.WriteLine((f * 100).ToString("F1"));

例如 f = 0.4792 --> "47.9"

标准数字格式的列表和说明可在 MSDN 上找到,以防有帮助。

How about just the following?

Console.WriteLine((f * 100).ToString("F1"));

e.g. f = 0.4792 --> "47.9"

A list and descriptions of standard numeric format are available here on MSDN, in case it helps.

无声静候 2024-08-24 20:52:49

可以尝试

Console.WriteLine(f.ToString("p1").Replace('%',' '));

或者,如果您不想使用空格,请使用 Replace('%','\0')

编辑:如果您只能使用 ToString() 方法,您可以创建一个 NumberFormatInfo 对象,将百分比符号设置为空白并将其传递到该方法中。
NumberFormatInfo 位于 System.Globalization 命名空间中)
例如

    NumberFormatInfo myNewFormat = new NumberFormatInfo();
    myNewFormat.PercentSymbol = "";

    float f = 0.479f;

    Console.WriteLine(f.ToString("p1",myNewFormat));

Could try

Console.WriteLine(f.ToString("p1").Replace('%',' '));

Or, if you want don't want a space, use 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.

    NumberFormatInfo myNewFormat = new NumberFormatInfo();
    myNewFormat.PercentSymbol = "";

    float f = 0.479f;

    Console.WriteLine(f.ToString("p1",myNewFormat));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文