2009-09-30 3 views
0

Ayant quelques problème avec cette ...Comment puis-je vérifier si une chaîne contient un nombre inférieur à un nombre entier?

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

donne: "La chaîne d'entrée était pas dans un format correct."

Comment puis-je vérifier si le nombre à l'intérieur de la chaîne est inférieur à 10000?

Oh ouais: TotalCost retourne un ContentResult de type text/plain

Répondre

4

Première utilisation Int32.TryParse pour voir si la chaîne est un nombre qui tombe dans la gamme de Int32.

Si le résultat est un nombre, vous pouvez toujours le comparer à n'importe quelle limite que vous avez.

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

+1. Bonne réponse, et tu m'as battu. – David

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