浮点到字符串格式说明符
我有一些浮点值想要转换为字符串,我想在转换时保持格式相同,即 999.0000(float) -> 999.0000(字符串)。我的问题是,当这些值在小数点后包含任意数量的零时(如前面的示例所示),它们在转换为字符串时会被删除,因此我实际最终得到的结果是 999。
我查看了格式说明符对于 MSDN 上的 toString()
方法,RoundTrip ('R') 说明符看起来会产生我想要的结果,但它仅支持 Single、Double 和 BigInt 变量。浮点变量是否有这样的格式说明符?或者将值转换为双精度会更容易吗?
更新:为了清楚起见,我想保留尾随零的原因是因为我正在进行小数位的比较,即我正在比较两个值之间小数位后的位数。例如,1.00 和 1.00000 小数点后的位数不同。我知道这是一个奇怪的要求,这是为了工作,而且要求来自高层。
更新2-3-11:
我想得太难了,我正在从txt文件中读取数字,然后将它们解析为浮点数,我将修改程序以检查字符串值是小数还是整数数字。很抱歉浪费了您的时间,尽管这是非常有见地的。
I have some float values I want to convert to a string, I want to keep the formatting the same when converting, i.e. 999.0000(float) -> 999.0000(String). My problem is when the values contain an arbitrary number of zeroes after the decimal point, as in the previous example, they are stripped away when converting to a string, so the result I actually end up with is 999.
I looked at the format specifiers for the toString()
method on MSDN, the RoundTrip ('R') specifier looks like it will produce what I want, but it is only supported for Single, Double and BigInt variables. Is there a format specifier like this for float variables?? Or would it be easier to just convert the values to doubles?
UPDATE: Just for clarity, the reason why I want to keep the trailing zeroes is because I'm doing a comparison of decimal places, i.e. I'm comparing the number of digits after the decimal place between two values. So for example, 1.00 and 1.00000 have a different number of digits after the decimal point. I know it's a strange request, it's for work and the requirement is coming from on high.
UPDATE 2-3-11:
I was thinking about this too hard, I'm reading the numbers from a txt file and then parsing them as floats, I'm going to modify the program to check whether the string values are decimals or whole numbers. Sorry for wasting your time, although this was very insightful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
ToString()
与此格式一起使用:在格式末尾添加尽可能多的零。
Use
ToString()
with this format:Put as much zero as necessary at the end of the format.
首先,正如 Etienne 所说,
float
在 C# 中是单一
。它只是该数据类型的 C# 关键字。所以你绝对可以这样做:
其次,你已经多次提到了数字的“格式”;数字没有格式,它们只有值。 字符串有格式。这让我想知道:你拥有的这个有格式但不是字符串的东西是什么?我能想到的最接近的东西是十进制,它确实保持了自己的精度;然而,在这种情况下,简单地调用decimal.ToString就可以达到您想要的效果。
包含一些示例代码怎么样,以便我们可以准确地看到您在做什么,以及为什么它没有实现您想要的目标?
Firstly, as Etienne says,
float
in C# isSingle
. It is just the C# keyword for that data type.So you can definitely do this:
Secondly, you have referred a couple of times to the number's "format"; numbers don't have formats, they only have values. Strings have formats. Which makes me wonder: what is this thing you have that has a format but is not a string? The closest thing I can think of would be
decimal
, which does maintain its own precision; however, calling simplydecimal.ToString
should have the effect you want in that case.How about including some example code so we can see exactly what you're doing, and why it isn't achieving what you want?
您可以将格式字符串传递给 ToString 方法,如下所示:
如果您想查看更多修饰符,请查看 MSDN - 标准数字格式字符串
You can pass a format string to the ToString method, like so:
If you want to see more modifiers, take a look at MSDN - Standard Numeric Format Strings
在 C# 中,
float
是System.Single
的别名(有点像int
是System.Int32< 的别名) /代码>)。
In C#,
float
is an alias forSystem.Single
(a bit likeint
is an alias forSystem.Int32
).