2011-12-13 79 views
0

我在文本框這個數字「84,8441546842904」如何在84,8或84,84轉換按鈕點擊事件刪除電話號碼?從一個文本框

+0

使用驗證事件。和decimal.TryParse + decimal.ToString(「N2」)重新分配Text屬性。你會免費獲得舍入。 –

+0

@HansPassant,但用戶應該小心這種方法:ToString中的四捨五入與Math.Round中的四捨五入略有不同,默認爲MidpointRounding.AwayFromZero,而AFAIK則無法指定不同的行爲。 – phoog

+0

是的,Math.Round得到了錯誤,其餘的.NET框架正確。只有帳戶想要明確地將其錯誤舍入。 –

回答

3

如果這你的意思是你要的價值和它全面解析到一定的小數位數:

double value = Math.Round(double.Parse(textbox.Text), 2); 

將解析文本,並將其輪爲2位小數。您可能需要解析考慮到當地文化的數字格式時使用System.Globalization.CultureInfo對象。

http://msdn.microsoft.com/en-us/library/75ks3aby.aspx

+0

非常感謝!快捷方便! – jolly

1

它幾乎看起來像你想的數量修剪1或2精度(不是「」在一些國家,如美國的「」用?)。如果這是你以後,你可以用Double.Parse將其轉換爲Double,然後看看到字符串格式選項描述here對其進行格式化迴文本框。

0
double i = 0; 
if (double.TryParse(tbxNumber.Text,out i)) { 
    MessageBox.Show("number is " + i.ToString()); 
} 
1

我使用這種功能來驗證用戶輸入。

這種解決問題的方法也尊重用戶的文化數字格式!

namespace Your_App_Namespace 
{ 

public static class Globals 
{ 
    public static double safeval = 0; // variable to save former value! 

    public static bool isPositiveNumeric(string strval, System.Globalization.NumberStyles NumberStyle) 
    // checking if string strval contains positive number in USER CULTURE NUMBER FORMAT! 
    { 
     double result; 
     boolean test; 
     if (strval.Contains("-")) test = false; 
     else test = Double.TryParse(strval, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result); 
       // if (test == false) MessageBox.Show("Not positive number!"); 
     return test; 
    } 

    public static string numstr2string(string strval, string nofdec) 
    // conversion from numeric string into string in USER CULTURE NUMBER FORMAT! 
    // call example numstr2string("12.3456", "0.00") returns "12.34" 
    { 
     string retstr = 0.ToString(nofdec); 
     if (Globals.isPositiveNumeric(strval, System.Globalization.NumberStyles.Number)) retstr = double.Parse(strval).ToString(nofdec); 
     else retstr = Globals.safeval.ToString(nofdec); 
     return retstr; 
    } 

    public static string number2string(double numval, string nofdec) 
    // conversion from numeric value into string in USER CULTURE NUMBER FORMAT! 
    // call example number2string(12.3456, "0.00") returns "12.34" 
    { 
     string retstr = 0.ToString(nofdec); 
     if (Globals.isPositiveNumeric(numval.ToString(), System.Globalization.NumberStyles.Number)) retstr = numval.ToString(nofdec); 
     else retstr = Globals.safeval.ToString(nofdec); 
     return retstr; 
    } 
} 

// Other Your_App_Namespace content 

} 

    // This is the way how to use those functions 

    // function to call when TextBox GotFocus 
    private void textbox_clear(object sender, System.Windows.RoutedEventArgs e) 
    { 
     TextBox txtbox = e.OriginalSource as TextBox; 
     // save original value 
     Globals.safeval = double.Parse(txtbox.Text); 
     txtbox.Text = ""; 
    } 

    // function to call when TextBox LostFocus 
    private void textbox_change(object sender, System.Windows.RoutedEventArgs e) 
    { 
     TextBox txtbox = e.OriginalSource as TextBox; 
     // text from textbox into sting with checking and string format 
     txtbox.Text = Globals.numstr2string(txtbox.Text, "0.00"); 
    }