2016-08-19 68 views
0

我有这样的代码格式文本框C#

private void timer1_Tick(object sender, EventArgs e) 
    { 
    textBox_Machine_X_Axis.Text = adsClient.ReadAny(ActPos_X, typeof(Double)).ToString(); 
    ... 

输出看起来是这样的:

2489.24544554444-654.1

现在的问题,我如何格式化这个代码,以便输出看起来是这样的2489.245-0654.100

+0

可能重复的[字符串格式:负/正浮点数](http://stackoverflow.com/questions/4356185/string-formatting-negative-positive-floating-point-numbers) – AdrianHHH

回答

1

您可以使用string.format方法,如下所示:

string.Format("{0:0000.000}", -654.1) 

改变你原来的代码,这将是这样的:

textBox_Machine_X_Axis.Text = string.Format("{0:0000.000}", adsClient.ReadAny(ActPos_X, typeof(Double))); 
+0

噢,我的上帝正在工作,非常感谢:)) –

+0

将它标记为答案:) – Sameed

0

您可以在的ToString(指定格式)方法是这样的:

private void timer1_Tick(object sender, EventArgs e) 
{ 
    textBox_Machine_X_Axis.Text = adsClient.ReadAny(ActPos_X, typeof(Double)).ToString("0000.000"); 
} 

退房这msdn article欲了解更多信息。