Format numbers with floating points and sign in textbox in C#

后端 未结 2 640
小鲜肉
小鲜肉 2021-01-27 10:21

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

2条回答
  •  长发绾君心
    2021-01-27 11:27

    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);
    

提交回复
热议问题