1

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.

This is what I have tried:

            textbox1.Text = number1.ToString("#000.00000");
            textbox2.Text = number2.ToString("#000.00000");
            textbox3.Text = number3.ToString("#000.00000");
            textbox4.Text = number4.ToString("#000.00000");

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?

APerson
  • 8,140
  • 8
  • 35
  • 49
NESHOM
  • 899
  • 16
  • 46
  • possible duplicate of: http://stackoverflow.com/questions/348201/custom-numeric-format-string-to-always-display-the-sign – dodald Dec 16 '14 at 17:29
  • no my question is different than that one as I also want the floating points to be aligned! – NESHOM Dec 16 '14 at 17:30
  • Have you considered right-aligning the text? – itsme86 Dec 16 '14 at 17:33
  • Now you have two: http://stackoverflow.com/questions/8293392/format-decimal-value-to-string-with-leading-spaces – dodald Dec 16 '14 at 17:33
  • Try to use a fixed width font with the duplicate – Steve Dec 16 '14 at 17:34
  • No duplicate - the second link has nothing to do with sign! – NESHOM Dec 16 '14 at 17:40
  • The first deals with the sign, which you said didn't deal with the padding. The second deals with the padding. I've added an answer which I think covers all your concerns. – dodald Dec 16 '14 at 17:46

2 Answers2

2

This should do the trick:

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);
dodald
  • 603
  • 3
  • 8
  • if the number is 1.2, this changes it to 1.20000. Is there anyway to make remove the extra zeros? – NESHOM Dec 16 '14 at 18:00
0

Have you tried String.Format?

String.Format("{0:0.0000}", 123.4567); //will return "123.4567"
String.Format("{0:0.0000}", 123.45678); //will return "123.4568"
GPierre
  • 100
  • 11
  • As you even point out in your comments, the + sign is not displayed so the numbers will not line up negative values. – itsme86 Dec 16 '14 at 17:33
  • The whole doc can be found here http://www.csharp-examples.net/string-format-double/ He could use this format instead : String.Format("{0,10:0.0000}", number); – GPierre Dec 16 '14 at 17:37