2016-08-19 66 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欲瞭解更多信息。