C# 如何格式化从文本框中获取的值?

发布于 2024-10-24 22:38:12 字数 389 浏览 1 评论 0原文

这可能很简单,但我对这种语言仍然陌生。

我正在创建一个 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 技术交流群。

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

发布评论

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

评论(6

¢好甜 2024-10-31 22:38:12

您所要做的是将 TextBox 中的字符串解析为双精度型,然后使用 NumberFormatInfo 将其格式化回字符串。

要解析字符串,请使用:

Double d;
Double.TryParse("5.4", out d);

要格式化为您想要的格式,请查看这些文档并选择您需要的格式: 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 using NumberFormatInfo.

To parse the string, use:

Double d;
Double.TryParse("5.4", out d);

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

美人如玉 2024-10-31 22:38:12

TextBox 文本解析为双精度变量(使用 double.Parsedouble.TryParse)并尝试如下操作:

double d = 5.0d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));
d = 5.4d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));

Parse your TextBox text into a double variable (using double.Parse or double.TryParse) and try something like this:

double d = 5.0d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));
d = 5.4d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));
故人如初 2024-10-31 22:38:12

请尝试以下操作:

int number = 5;
string content = number.ToString("0000000.00000");

Try the following:

int number = 5;
string content = number.ToString("0000000.00000");
琉璃繁缕 2024-10-31 22:38:12

这篇博客文章很好地介绍了如何使用 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”)格式说明符”下:

Console.WriteLine(value.ToString("D8"));
// Displays -00012345

看看该 MSDN 链接,在标题“定点(“F”)格式说明符”下

integerNumber = -29541;
Console.WriteLine(integerNumber.ToString("F3", 
                  CultureInfo.InvariantCulture));
// Displays -29541.000

但实际上,要实现您的目标正在寻找,那么您可能最好使用如下格式字符串:

var f = 5.4;
f.ToString("000000000.0000000000000");
// Displays 000000005.4000000000000

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":

Console.WriteLine(value.ToString("D8"));
// Displays -00012345

Take a look at that MSDN link, under heading "The Fixed-Point ("F") Format Specifier"

integerNumber = -29541;
Console.WriteLine(integerNumber.ToString("F3", 
                  CultureInfo.InvariantCulture));
// Displays -29541.000

But actually, to achieve what you're looking for then you are probably best using a format string like:

var f = 5.4;
f.ToString("000000000.0000000000000");
// Displays 000000005.4000000000000
好多鱼好多余 2024-10-31 22:38:12

首先,文本框为您提供(文本)字符串形式的值。如果您想将其作为数字进一步处理(您确实这样做了),您首先需要转换(解析)它。然后,您只需按照您想要的方式设置数字格式

var number = Decimal.Parse(textBox.Text);
writer.Write(number.ToString("{0:0000000.00000}", number));

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:

var number = Decimal.Parse(textBox.Text);
writer.Write(number.ToString("{0:0000000.00000}", number));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文