TheMadMonk

Hi all,

I need to parse a string which is in the following format (example): -0.0004 USD/Tonnes

Basically, it will be a double value, might be preceded by the negative (-) sign

and followed by <Currency>/<Unit of Measure> .

The result should be just the double value (with the sign if its there), as in the above example, it should translate to -0.0004

I wanted to know if there is an easy way of doing it in C#, like using the Double.Parse() or the string.Format() methods (I tried them both but couldn't get them to work)

Or do I have to parse the string character-by-character to get the required result

Thanks in advance,

MadMonk

P.S.: Apologies if its a silly question, I'm still a newbie in .NET



Re: Regular Expressions Parsing double value from a string

ahmedilyas

im not sure about the formatting etc... but you can use the parse or rather try parse in .NET 2.0 to see if it can parse the value to a double.

you would have to remove the text though, this is why it's failing probably. I'm sure there may well be a way to get just the numbers on its own without having to say, using Strings and SubStrings etc... however if you just have the number: -0.0004:

double theResult = 0;

if (double.TryParse(theString, out theResult))

{

//successfully parsed the number to double and stored in theResult

}

I'll see what I can dig up about how to just get the numbers on its own without doing the SubString stuff






Re: Regular Expressions Parsing double value from a string

TheMadMonk

Hi ahmedilyas,

Yes, I'm aware of the double.TryParse & Parse methods, but my problem is how to get just the number without the text part at the end.

Somehow, substring seems to be an awkward solution and I was expecting .NET 2.0 to have some easy way of accomplishing this.

Thanks,

MadMonk





Re: Regular Expressions Parsing double value from a string

ahmedilyas

I understand. For now all I can think about is to go through each character, check if its a non numeric character, if not, then you know that thats all there is in terms of the numbers collections.

System.Text.StringBuilder theNumbers = new System.Text.StringBuilder();

foreach(Char curChar in theString)

{

   if (!Char.IsNumber(curChar))

   {

      break;

   }

 

      theNumbers.Append(curChar);

}

 

now you have all the numbers up until the first non number character, stored in the StringBuilder "theNumbers". I'm sure you are aware of this and want to avoid it.






Re: Regular Expressions Parsing double value from a string

TheMadMonk

Yes, you are right, I was aware of the logic which you used in your code snippet, just wanted to avoid it and use something more elegant, if available.

Thanks a lot for your help anyway... :-)





Re: Regular Expressions Parsing double value from a string

dreadjr

Can't you just split the string and do a double.parse on the first half of the split.

You could also use a regular expression to match the number part and do a double.parse on that.






Re: Regular Expressions Parsing double value from a string

ahmedilyas

doing Regex would be overkill hence why I did not suggest it :-) Regex is expensive and should be used whenever you have a fair amount of pattern matching to do. Nothing is stopping you from using Regex, just trying to think more of an effecient/best way if possible




Re: Regular Expressions Parsing double value from a string

dreadjr

In your solution if the number is negative, it starts with a '-' which is not considered a number. Therefore wouldn't return the number portion. Regex is overkill but sometimes elegance and overkill co-inside.




Re: Regular Expressions Parsing double value from a string

TheMadMonk

Ok.... here's what I'm using as of now....

public static string parseDouble(string doubleWithUnit)
{
System.Text.StringBuilder theNumbers = new System.Text.StringBuilder();

foreach (Char curChar in doubleWithUnit)
{
if ((!Char.IsNumber(curChar))&&(curChar.CompareTo('.') != 0)&&(curChar.CompareTo('-') != 0))
{
break;
}
theNumbers.Append(curChar);
}
return theNumbers.ToString();
}





Re: Regular Expressions Parsing double value from a string

dreadjr

This seems like overkill, becuase look how many comparisions you are making. It really would be simplier to just split the string on a space and use double.parse to parse the first half (the number)




Re: Regular Expressions Parsing double value from a string

OmegaMan

After finding this post and reading it, here are my two cents. Regular Expressions is the perfect tool for what you needed. The cost overhead is neglible and I use it to do what you describe on a daily basis. If you are still interested, I can provide you with a regex and how to use it. The syntax is arcane, but powerful and once one starts to use it, it becomes an indepensible tool.






Re: Regular Expressions Parsing double value from a string

TheMadMonk

Hi,

I'd appreciate the sample. However, my source has now changed to include (in some cases) some market indices also.

Here are some examples:

"NASDAQ TOP 30 - 0.33 USD/Tonnes"

"NASDAQ TOP 30 + 0.58 USD/MWh"

" - 1.66 USD/BBL"

" 12.54 USD/Tonnes"

Out of this, I just want the double values (-0.33, 0.58, -1.66 and 12.54).

Thanks





Re: Regular Expressions Parsing double value from a string

OmegaMan

Are the last two representational of the items or just cut-outs Give an example list of data, no quotes.





Re: Regular Expressions Parsing double value from a string

TheMadMonk

The last two are also examples of source data.

Here's the example list:

NASDAQ TOP 30 - 0.33 USD/Tonnes

NASDAQ TOP 30 + 0.58 USD/MWh

- 1.66 USD/BBL

12.54 USD/Tonnes





Re: Regular Expressions Parsing double value from a string

Marcelo Guerra - MSFT

Hi,

This seems to solve the problem

        string source = " NASDAQ TOP 30 - 0.33 USD/Tonnes\r\nNASDAQ TOP 30 + 0.58 USD/MWh\r\n- 1.66 USD/BBL\r\n12.54 USD/Tonnes";< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        MatchCollection matches = Regex.Matches(source, @".* ([-]{0,1} *\d+.\d+)");

        List<double> doubles = new List<double>();

        foreach (Match match in matches)

        {

            string value = match.Groups[1].Value;

            value = value.Replace(" ", "");

            doubles.Add(double.Parse(value));

        }   

I may be missing some other case.