Solved

c# determine if a 50 character string is a number

Posted on 2014-03-14
10
279 Views
Last Modified: 2014-03-18
I am creating an excel document and need a value in a cell to be stored as a string.  If it is a number I need to put an apostrophe in front of it.  I was using decimal.tryparse but that won't work with a 50 digit number.  How can I determine if a string is a number no matter how many characters or digits are there?  I also need it to work with a negative sign, commas and a decimal point.

(Please do not ask me to change the way I am creating the excel, that is not important and is not the question.)

Thanks!

decimal out_dec = 0;
//this will always evaluate to false
var isNumber decimal.TryParse("12345678901234567890123456789012345678901234567890", out out_dec)

Open in new window

0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39930393
You could check each digit:

public bool IsNumeric(string value)
{
    for (int i = 0; i < value.Length; i++)
    {
        if (i == 0 && value[i] == '-') continue;

        if (!char.IsDigit(value[i]))
        {
            return false;
        }
    }

    return true;
}

Open in new window

0
 

Author Comment

by:jackjohnson44
ID: 39930416
That won't work with decimal points, and commas.  I need a way to determine if it is a real number, not just 0-9, comma, decimal point, and negative sign

for example
100,000 is a number
10,00 is not a number
1.2.3 is not a number
etc...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39930425
OK, I overlooked that bit. Modified:

public bool IsNumeric(string value)
{
    bool decimalFound = false;

    for (int i = 0; i < value.Length; i++)
    {
        if (i == 0 && value[i] == '-') continue;

        if (value[i] == '.')
        {
            if (decimalFound)
            {
                return false;
            }

            decimalFound = true;
        }
        else if (!char.IsDigit(value[i]))
        {
            return false;
        }
    }

    return true;
}

Open in new window

0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 35

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39930483
If you can use regex, you can use this:
@"^-?[0-9]*(,[0-9]{3})*\.?[0-9]*$"

Open in new window

Works correctly for the following cases:
100,000
10,00
1.2.3
50
100000.58796
-50
-23412.6535
-5.3.2

Open in new window


Borrowing the above function name, you could use something like this:
public bool IsNumeric(string value)
{
    string pattern = @"^-?[0-9]*(,[0-9]{3})*\.?[0-9]*$";

    Regex r = new Regex(pattern);
	
	Match m = r.Match(value)
	
	if (m.Success) {
	
		return true;
		
	}

    return false;
}

Open in new window

HTH,
Dan
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39930491
Dan Craciun's pattern also accepts (in case it's relevant):

[nothing]
1. [no fractional part]
100000000,000.00 [partially formatted]
,000.00 [partially formatted]
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39930500
OK, thanks for that.

1. and ,000.00 are easy to solve. Still needs work on the 10000,000.00 part :)

@"^-?[0-9]+(,[0-9]{3})*(\.[0-9]+)?$"

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39930501
Try {1,3} rather than +   ; )
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39930520
Yeah, but that's only if the numbers were always grouped by ",". Since that was not the case, I need to use an if (if there is at least an , try to match groups of 3, if not, search for digits only):
@"^-?(?(?=.*,)([0-9]{1,3}(,[0-9]{3})*)|([0-9]*))(\.[0-9]+)?$"

Open in new window

works correctly for:
100,000
10,00
1.2.3
50
100000.58796
-50
-23412.6535
-5.3.2
1.
10000000,000.00
,000.00

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39930533
You don't need the if...a simple alternation will suffice.

e.g.

^-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39930556
Yup, thank you for that. I'm still new at regular expressions and when I don't understand something I fall back to the programmer's way of thinking :)

BTW, I recently found out that  \d is looking for a lot more characters than [0-9], so in the case of long strings it's faster to use [0-9], although I agree it's easier to read \d.

Here are all the characters \d is looking for: http://www.fileformat.info/info/unicode/category/Nd/list.htm
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 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