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

后端 未结 2 639
小鲜肉
小鲜肉 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:12

    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"
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题