C# 如何格式化从文本框中获取的值?
这可能很简单,但我对这种语言仍然陌生。
我正在创建一个 Windows 应用程序,其中有一个文本框和一个按钮。它将执行的操作是获取在文本框中输入的值并创建一个 .txt 文件,并将文本框中输入的值作为其内容。
我就这么干过我已成功将“5”添加到文本框并按下按钮,它将创建一个内容为“5”的txt 文件。
我的问题是,如何将文本框中的值格式化为 xxxxxx.xxxxx 之类的内容?
那么,如果我输入 5
如何使其在文本中创建为 0000005.00000
?
或者如果我输入5.4
我该如何让它变成这样0000005.40000
?
任何人都可以阐明吗?或者编码示例?
this may be simple but im still new to this language.
I'm creating a windows application where is a text box and a a button. What it will do is take the value entered in the text box and create a .txt file with the value entered in the textbox as its content.
I've don this. I have successfully add "5" to the textbox and pressed the button and it will create a txt file with content "5" in it.
My question is, how do I format the value form the text box to something like this xxxxxxx.xxxxx?
So if I enter 5
how do I make it create in text to become 0000005.00000
?
Or if I enter 5.4
how do i make it become like this 0000005.40000
?
Can anyone shed a light? Or coding sample?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您所要做的是将
TextBox
中的字符串解析为双精度型,然后使用NumberFormatInfo
将其格式化回字符串。要解析字符串,请使用:
要格式化为您想要的格式,请查看这些文档并选择您需要的格式: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
What you have to do is to parse the string from the
TextBox
into a double and then format it back to string usingNumberFormatInfo
.To parse the string, use:
To format to what you want, have a look at these docs and choose the format that you need: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
将
TextBox
文本解析为双精度变量(使用double.Parse
或double.TryParse
)并尝试如下操作:Parse your
TextBox
text into a double variable (usingdouble.Parse
ordouble.TryParse
) and try something like this:请尝试以下操作:
Try the following:
这篇博客文章很好地介绍了如何使用 string.Format - http://blog.stevex .net/string-formatting-in-csharp/
另外,MSDN 也非常有帮助 - http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
例如,“如果使用我输入 5.4,我如何使它变成这样 0000005.40000?”:
看一下在该 MSDN 链接,标题“十进制(“D”)格式说明符”下:
看看该 MSDN 链接,在标题“定点(“F”)格式说明符”下
但实际上,要实现您的目标正在寻找,那么您可能最好使用如下格式字符串:
This blog post is good for how to use string.Format - http://blog.stevex.net/string-formatting-in-csharp/
Also MSDN is very helpful - http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
For example, "if using i enter 5.4 and how do i make it become like this 0000005.40000?":
Take a look at that MSDN link, under heading "The Decimal ("D") Format Specifier":
Take a look at that MSDN link, under heading "The Fixed-Point ("F") Format Specifier"
But actually, to achieve what you're looking for then you are probably best using a format string like:
首先,文本框为您提供(文本)字符串形式的值。如果您想将其作为数字进一步处理(您确实这样做了),您首先需要转换(解析)它。然后,您只需按照您想要的方式设置数字格式:
First of all, a text box gives you the value as a (text) string. If you want to process it further as a number (which you do), you first need to convert (parse) it. Then, you just format the number the way you want:
查找
string.Format()
:http://alexonasp.net/samples/stringformatting /
Look up
string.Format()
:http://alexonasp.net/samples/stringformatting/