I can't seem to get a simple test for numeric to work...

Posted on 2012-09-18
Last Modified: 2012-09-18
This is my object's value I want to test:  0.25 or .25 (either fail)

This is my code, which fails every time there is a decimal point in the value:

                    NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint,
                    CultureInfo.CurrentCulture, out x))
                    return true;
                    return false;

Open in new window

Any ideas why? or how to fix it?
Question by:rmmarsh
    LVL 21

    Accepted Solution

    By "fail" you mean that if statement is resulting in false?  How about reorganizing it to isolate the confusion:

    decimal x;
    decimal c = new decimal(.25);
    string value = c.ToString();
    bool btest = Decimal.TryParse(value,
                        NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint,
                        CultureInfo.CurrentCulture, out x);
                        return true;
                        return false;

    And verify what btest is giving in the debugger, and if that still fails look at what "value" actually is getting.

    Author Comment

    The data (value) is coming from a Firebird database and can be a string or decimal.  I can't determine what the value is going into your code.  I think I'm going to use a RegEx expression to check for numeric... I'll get back to you...

    Author Closing Comment

    There is no way I can tell what is coming into the method... I wound up using RegEx and it now works in all cases.  

    Thank you for taking the time to suggest something.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Help file 1 28 Application cannot refer to dlls 3 24
    How to host windows service on Windows Server 2008 3 21
    c# string 6 27
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now