0

I need to find out what value the user has entered into a textbox.

The user could've entered an integer, double or decimal number into the textbox.

Which one should I use to validate the value?

Double.Parse(txtboxNo.text) 
int.Parse(txtboxNo.text);
Decimal.Parse(txtboxNo.text)

I tried this (if user enter 1 or 1.8, this function still work):

public bool IsNumeric(string strNbr)
{
    Double d_Nbr;

    try
    {
        d_Nbr = Double.Parse(strNbr);
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

My problem: I am working on a mobile Sales App. The sales man has the rights to change the price. Salesman want to work fast. So, I need to detect if he enter a correct price ( my price is decimal) what if he enter : example: 1 or 1.0 or 23 or 333.0 or press for fun 123456 . How I handle ?

MilkBottle
  • 4,242
  • 13
  • 64
  • 146
  • 3
    What are you doing with the value? Asking because this does not seem like an actual problem you would face. Also, what is the difference between the string representation of Double and Decimal? – Joel Peltonen Sep 23 '14 at 06:45
  • Check the "is" keyword of the framework. – Nahuel Ianni Sep 23 '14 at 06:47
  • If they can enter a decimal then `int.parse` will error, personally I'd use the correct `TryParse` – Sayse Sep 23 '14 at 06:47
  • This question has a neat answer for your problem: http://stackoverflow.com/questions/16835750/fastest-way-to-check-if-a-string-can-be-parsed – Marco Sep 23 '14 at 06:48
  • 2
    @Nenotlep "2e4" is valid input for `Double.Parse` but not for `Decimal.Parse`. – Dirk Sep 23 '14 at 06:49
  • @Nenotlep - About 15 digits – Sayse Sep 23 '14 at 06:50
  • 3
    You should explain to us what is your decision/preference about a user typing "1" in your textbox. This dilemma has no sense. You should worry about the intended usage of that input and act accordingly. – Steve Sep 23 '14 at 06:54
  • If one takes a normal floating point number (without shortcuts) like 1.3 it is impossible to differentate if it is meant as double or decimal. so the question is also why differentate between those two (thus what are the reasons behind this differentation and if it is not better to just alwayss taking one specific of them instead of trying to differentate) so why are you differentating there @MilkBotte ? – Thomas Sep 23 '14 at 06:55
  • Also on the terms of decimal point values.........can it be that the users type in 1000er separators? (thus 1.000.000,00 or 1,000,000.00) as that can complicate things quite a bit (thus comparisons) – Thomas Sep 23 '14 at 07:13
  • Hi all, I am working on a mobile Sales App. The sales man has the rights to change the price. Salesman want to work fast. So, I need to detect if he enter a correct price ( my price is decimal) what if he enter : example: 1 or 1.0 or 23 or 333.0 or press for fun 123456 . How I handle ? – MilkBottle Sep 24 '14 at 02:34
  • If your price is a decimal, then *always* `Decimal.TryParse`. It will then reject things like `2e4`, but accept `23` or `23.0`, which seems exactly what you want. – Martijn Sep 29 '14 at 13:09

3 Answers3

1

I'm not sure about the purpose but you may try the TryParse() Method of Double/Int/Decimal to Validate the string Value.

if (Double.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("{0} is outside the range of a Double.",

MSDN article: http://msdn.microsoft.com/en-us/library/994c0zb1%28v=vs.110%29.aspx

Zalem
  • 154
  • 1
  • 10
1

Differentating between an int and a double/decimal is not much of a problem you would only need to test if it has a decimal point. The problem only starts as soon as you try to differentate between double and decimal as both have decimal points and the only difference is the precision as can be seen here: Difference between decimal, float and double in .NET?

One possibility I can see is that you try to look for a decimal point and then see if it has enough digits to be seen sa decimal rather than float. String textBoxValue = txtboxNo.Text.Replace(',', '.');

if (textBoxValue.IndexOf(".") >= 0) 
{
    // double or decimal
    if (textBoxValue.length >= 17)
    {
        // Possible decimal as too long for double
        Decimal mydec;
        if (Decimal.TryParse(textBoxValue , out mydec))
        {
           // Is decimal
        }
       else
       {
           // Is something else
       }
    } 
    else
    {
       double mydoub;
       if (Double.TryParse(textBoxValue, out mydoub))
       {
           // Is double
       }
       else
       {
           // Is something else
       }
    }
}
else
{
    int myInt;
    // possible int 
    if (int.TryParse(textBoxValue, out myInt))
    { 
        // its an int
    }
    else
    {
        // something else
    }
}
Community
  • 1
  • 1
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • @Steve - Good point but the IndexOf decimal point would stop that from happening here – Sayse Sep 23 '14 at 06:57
  • Ah overlooked what steve meant there for a second. Yepp that is why I put the indexof first and only then differentated between double and decimal to avoid that problem – Thomas Sep 23 '14 at 06:58
  • 1
    the '.' depends on the selected culture. – blfuentes Sep 23 '14 at 06:59
  • @Corak steve has a point there as "1" would return true for double despite it being an integer. I checked for that with the indexof though first and made sure double/decimal are only checked if it cant be an integer (normally I would try int.tryparse instead of indexof but I've seen that fail in teh past telling me a double is an integer so instead I'm using indexof to make sure first it is a decimal point value that I'm looking at]) – Thomas Sep 23 '14 at 07:00
  • My point is: A string representing a number and that doesn't contain a decimal point is still a possible double. So the question makes no sense without knowing the reason of the input. But if you know the reason of the input you use the appropriate type for your purposes. You answer is correct but you must assume that if there is no decimal point then it is an integer. – Steve Sep 23 '14 at 07:00
  • @blacai good point there. will modify the answer to take also "," as decimal point into account (can't use the culture directly as I've seen that fail because someone installed an english version of the OS but is using the german , notation for numbers) – Thomas Sep 23 '14 at 07:03
  • How about: `if (Math.Abs(doubleValue - Math.Floor(doubleValue)) < someEpsilon && doubleValue <= Int32.MaxValue && doubleValue >= Int32.MinValue) { // is int }`? – Corak Sep 23 '14 at 07:04
  • @Corak what does that do that int.tryparse doesn't? – Thomas Sep 23 '14 at 07:10
  • @Thomas - "I would try int.tryparse instead of indexof but I've seen that fail in teh past" – Corak Sep 23 '14 at 07:11
  • ah that you meant. with "fail" I meant the differentation between an int and a number with a decimal point (although the problem had vanished after reinstalling visual studio back then it has made me overcautious in these regards) – Thomas Sep 23 '14 at 07:12
-1

You may use*

double output;
if(double.tryParse(txtboxNo.Text,output))
{
 // it's double
}
else{// not double }
Ahmed Abaza
  • 89
  • 1
  • 7
  • I think it would be more helpful for the op and further visitors, when you add some explaination to your intension (In example: Explan, how the method tryParse() works). – Reporter Sep 29 '14 at 12:14
  • also I don't understand why you people always rating -1 -1 ! – Ahmed Abaza Sep 29 '14 at 20:41