jolly
jolly

Reputation: 285

Remove number from a textbox

I have this number in textbox "84,8441546842904" how to convert in 84,8 or 84,84 on button click event?

Upvotes: 0

Views: 425

Answers (4)

Einotes Team
Einotes Team

Reputation: 11

I use this kind of functions to validate user input.

This approach to the problem also respects user culture number format!

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");
    }

Upvotes: 1

William Melani
William Melani

Reputation: 4268

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

Upvotes: 0

nealyoung
nealyoung

Reputation: 88

If by this you mean you want to parse the value and round it to a certain number of decimal places:

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

will parse the text and round it to 2 decimal places. You may need to use a System.Globalization.CultureInfo object when parsing to account for your local culture's number formatting.

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

Upvotes: 3

M.Babcock
M.Babcock

Reputation: 18965

It almost looks like you are trying to trim the number to 1 or 2 precision (isn't the ',' used in some countries as the US '.'?). If this is what you're after, you can use Double.Parse to convert it to a Double and then look into the string format options described here to format it back to the textbox.

Upvotes: 1

Related Questions