浮动点和登录文本框在C#格式的数字 [英] Format numbers with floating points and sign in textbox in C#

查看:120
本文介绍了浮动点和登录文本框在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#格式的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆