C#:如何强制数字格式字符串中的尾随零?
我需要将浮动显示为
1.00
1.50
1.55
1.60
以下是我使用 f2 格式看到的内容。
1.
1.5
1.55
1.6
有没有办法强制出现尾随0?
(我正在使用 DevExpress SpinEdit 控件并尝试设置显示和编辑格式。)
I need to display float as
1.00
1.50
1.55
1.60
The following is what I see using f2 format.
1.
1.5
1.55
1.6
Is there a way to force the trailing 0 to appear?
(I'm using a DevExpress SpinEdit control and trying to set the display and edit format.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用如下语法:
You can use syntax like this:
为了将来参考,
http://www.csharp-examples.net/string-format-双/
For future reference,
http://www.csharp-examples.net/string-format-double/
在极少数情况下,我需要格式化,我会转到这里:
http:// /blog.stevex.net/index.php/string-formatting-in-csharp/
On those rare occasions I need to formatting, I go here:
http://blog.stevex.net/index.php/string-formatting-in-csharp/
不过,将来我建议搜索 Dev Express 知识库 或通过电子邮件发送支持( [电子邮件受保护])。 他们将能够在大约一天内回答您的问题。
In the future, though, I would recommended searching Dev Express' knowledge base or emailing support ([email protected]). They'll be able to answer your question in about a day.
您还可以通过字符串插值来完成此操作(请注意,这是 C# 6 及更高版本):
You can also do this with string interpolation (note that this is C# 6 and above):
假设您有一个名为“myNumber”的 double 类型变量:
而不是:
我会选择:
因为 N2 将添加“,”数千个分隔符,这通常会导致下游出现问题。
Let's say you had a variable called "myNumber" of type double:
Instead of:
I would opt for:
since N2 will add "," thousands separators, which can often cause problems downstream.