如何将值格式化为不带百分号的百分比?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以我假设您被
Float.ToString(String)
困住了,并且您不能只编辑f.ToString("p1")< 中的
p1
/代码>。现在这是一个棘手的问题。如果您不担心破坏与隐含更改相关的任何内容,则可以执行以下操作:“P”数字格式,如文档所述 在 MSDN 上,使用
NumericFormatInfo.PercentSymbol
来写入“%”符号。NumericFormatInfo
是当前CultureInfo
的成员。您可以做的是克隆您的CurrentCulture
,并将 PercentSymbol 修改为""
,如下所示:如果您不想更改,这将是可行的方法ToString 调用。
So I assume that you are stuck with
Float.ToString(String)
, and that you cant only edit thep1
inf.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 currentCultureInfo
. What you could do, is clone yourCurrentCulture
, and modify the PercentSymbol to""
like the following:This would be the way to go if you don't want to alter the ToString call.
仅以下内容怎么样?
例如 f = 0.4792 --> "47.9"
标准数字格式的列表和说明可在 MSDN 上找到,以防有帮助。
How about just the following?
e.g. f = 0.4792 --> "47.9"
A list and descriptions of standard numeric format are available here on MSDN, in case it helps.
可以尝试
或者,如果您不想使用空格,请使用
Replace('%','\0')
编辑:如果您只能使用
ToString()
方法,您可以创建一个NumberFormatInfo
对象,将百分比符号设置为空白并将其传递到该方法中。(
NumberFormatInfo
位于System.Globalization
命名空间中)例如
Could try
Or, if you want don't want a space, use
Replace('%','\0')
EDIT: If you can only use the
ToString()
method, you could create aNumberFormatInfo
object, set the percentage symbol to be blank and pass it into the method.(
NumberFormatInfo
is in theSystem.Globalization
namespace)e.g.