Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2012-09-18
Medium Priority
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
  • 2
LVL 21

Accepted Solution

mastoo earned 2000 total points
ID: 38410005
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

ID: 38410165
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

ID: 38410363
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.

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

810 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