2011-12-13 80 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"); 
    }