-2

i'm currently doing a C# WPf application. May i know how to validate for the string value? Example "0.00" or "-1.00",etc?

Because both of them is return as a string that i retrieve from SAP, is there anyway to check?

Sorry I'm still new to C# , not really familiar with the function that have in the C#.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Marcus Tan
  • 407
  • 1
  • 9
  • 26
  • Validate it based on what rules? What aspect do you want validated? – Jeroen Vannevel Jun 25 '15 at 09:27
  • 1
    negative string means? do you want to check the content is a number and wether its negative? – Joseph Jun 25 '15 at 09:29
  • Have a look at `decimal.Parse` and `decimal.TryParse` – germi Jun 25 '15 at 09:30
  • @JeroenVannevel Actually i was trying to check for current balance left, if the SAP return me positive balance then i proceed, if it return negative value then i stop the process. Example "Current Balance : 10.00" then proceed , else if "Current Balance: -15.00" then exit program – Marcus Tan Jun 25 '15 at 09:30
  • Dending where you do it, it looks like floating numbers, so you could try do to Double.TryParse("the string",out doubleVariable) it returns a booleans if it is a double or not – Henrik Bøgelund Lavstsen Jun 25 '15 at 09:30
  • @MarcusTan If thats the Case i would look into regualar expression, because the value is mixed with characters – Henrik Bøgelund Lavstsen Jun 25 '15 at 09:32
  • Anyway, I'm not checking whether the string is number or not. What i wanted to check is whether the string that return is negative value or not. Please don't simply mark this is duplicate or what... – Marcus Tan Jun 25 '15 at 09:33
  • You may check the result from `decimal.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out temp_decimal)`. InvariantCulture is necessary if your string is always point sign. – i486 Jun 25 '15 at 09:35

3 Answers3

1

I assume you want to check if the number that you get back is a negative number?

double number = 0;
if(double.TryParse(myString,out number)){
   if (number > 0)
       \\Do something
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • Yes, you are right. I wanted to check the number whether i get is negative number or not. Unlike those simply mark the question withoutht understand the question asked. Let me try it out firsts. thanks for your answer – Marcus Tan Jun 25 '15 at 09:34
  • and for your correction it was if(double.TryParse(myString,out number)) – Marcus Tan Jun 25 '15 at 09:41
0

you can use TryParse() as bellow

    string s="0.00";
    double dvalue=0;
    if(!double.TryParse(s,out dvalue)){
       //not valid
    }
Bellash
  • 7,560
  • 6
  • 53
  • 86
0

Do you always have a double value? Try

Double.Parse(...)

edit: corrected method

Pedro Isaaco
  • 404
  • 3
  • 10