MetaGuru
MetaGuru

Reputation: 43873

How can I check if a string contains a number smaller than an integer?

Having some issue with this...

    if (System.Convert.ToInt32(TotalCost(theOrder.OrderData.ToString()).ToString()) < 10000)
        ViewData["cc"] = "OK";
    else
        ViewData["cc"] = "NO";

yields: "Input string was not in a correct format."

How can I check if the number inside the string is less than 10000?

Oh yeah: TotalCost returns a ContentResult of type text/plain

Upvotes: 0

Views: 667

Answers (3)

bobbymcr
bobbymcr

Reputation: 24177

int value = Convert.ToInt32(TotalCost(theOrder.OrderData.ToString()));
if (value < 10000)
{
    // ...
}

Upvotes: 0

David
David

Reputation: 73604

use Int32.TryParse()

Upvotes: 1

Mark Seemann
Mark Seemann

Reputation: 233487

First use Int32.TryParse to see if the string is a number that falls into the range of Int32.

If the result is a number, you can always compare it to whatever limit you have.

int i;
if (int.TryParse(theOrder.OrderData, out i))
{
    if (i < 10000)
    {
       // Do stuff...
    }
}

Upvotes: 4

Related Questions