Format numbers with floating points and sign in textbox in C#(用浮点数格式化数字并在 C# 中登录文本框)
问题描述
我有多个垂直左对齐的文本框.它们显示带有浮点数和符号的数字.数字在不断变化.我想固定浮点的位置,以便在更改数字时浮点的位置保持不变,并在所有文本框中保持垂直对齐.
I have multiple textboxes left-aligned vertically. They show numbers with floating points and signs. The numbers are continuously changing. I'd like to make the position of the floating point fixed so that when the numbers are changed the position of the floating points are unchanged and keep aligned vertically in all of the textboxes.
这是我尝试过的:
textbox1.Text = number1.ToString("#000.00000");
textbox2.Text = number2.ToString("#000.00000");
textbox3.Text = number3.ToString("#000.00000");
textbox4.Text = number4.ToString("#000.00000");
当数字为负数并且我看到 - 开始时,它可以工作,但是当它们为正数时,数字会向左移动.我可以手动在正数的开头添加空格或 + 号,但我想知道是否有更优雅的方法.此外,当数字像 3.2 时,这会将它们更改为 003.20000,是否有办法将额外的零更改为空格?
It works when the numbers are negative and I see - sign in the beginning, but when they are positive the numbers are shifted to the left. I can manually add space or + sign to the beginning of the positive numbers, but I am wondering if there a more elegant approach for this. Also when the numbers are like 3.2, this will change them to 003.20000, Is there anyway to make it so that the additional zeros are changed to space?
推荐答案
这应该可以解决问题:
textbox1.Text = String.Format("{0,10:+0.00000;-0.00000}", number1);
textbox2.Text = String.Format("{0,10:+0.00000;-0.00000}", number2);
textbox3.Text = String.Format("{0,10:+0.00000;-0.00000}", number3);
textbox4.Text = String.Format("{0,10:+0.00000;-0.00000}", number4);
textbox5.Text = String.Format("{0,10:+0.00000;-0.00000}", number5);
或者如果你想要在填充之前的符号符号.
or this if you want the sign symbol before the padding.
textbox1.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number1);
textbox2.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number2);
textbox3.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number3);
textbox4.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number4);
textbox5.Text = String.Format("{0:+;-}{0,9:0.00000;0.00000}", number5);
这篇关于用浮点数格式化数字并在 C# 中登录文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用浮点数格式化数字并在 C# 中登录文本框
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
