2012-02-07 14 views
5

Sto riscontrando qualche problema nel convalidare un input di denaro. Ho usato alcuni suggerimenti dalle mie altre domande per scrivere un codice migliore. Quello che segue è ciò che sto usando per convalidare se l'input è o meno denaro.Convalida i miei soldi

static void Main(string[] args) 
{ 
    string myTest1 = "$1,234.56"; 
    string myTest2 = "$1.00"; 
    string myTest3 = "$1000.01"; 
    string myTest4 = "$1,234,567.89"; 

    myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator(); 

    bool myResult1 = Miimv.isMaybeMoney(myTest1); 
    bool myResult2 = Miimv.isMaybeMoney(myTest2); 
    bool myResult3 = Miimv.isMaybeMoney(myTest3); 
    bool myResult4 = Miimv.isMaybeMoney(myTest4); 
} 

public bool isMaybeMoney(object theirMaybeMoney) 
{ 
    string myMaybeMoney = theirMaybeMoney.ToString(); 

    if (myMaybeMoney.StartsWith("-")) 
    { 
     myMaybeMoney.Remove(0, 1); 
    } 

    if (!myMaybeMoney.StartsWith("$")) 
    { 
     return false; 
    } 

    myMaybeMoney.Remove(0, 1); 

    string[] myMaybeMoneyStringArray = myMaybeMoney.Split('.'); 

    string myMaybeMoneyDollars = myMaybeMoneyStringArray[0]; 
    string myMaybeMoneyCents = myMaybeMoneyStringArray[1]; 

    if (!isDollars(myMaybeMoneyDollars)) 
    { 
     return false; 
    } 

    if (!isCents(myMaybeMoneyCents)) 
    { 
     return false; 
    } 

    return true; 
} 

private bool isDollars(string theirMaybeMoneyDollars) 
{ 
    if (!isNumber(theirMaybeMoneyDollars)) 
     return false; 

    try 
    { 
     int myMaybeDollars = Convert.ToInt32(theirMaybeMoneyDollars); 

     if (myMaybeDollars < 1) 
      return false; 

     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 

    return true; 
} 

private bool isCents(string theirMaybeMoneyCents) 
{ 
    if (!isNumber(theirMaybeMoneyCents)) 
     return false; 

    try 
    { 
     int myMaybeCents = Convert.ToInt32(theirMaybeMoneyCents); 

     if (myMaybeCents > 99) 
      return false; 
     if (myMaybeCents < 1) 
      return false; 

     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 

    return true; 
} 

private bool isNumber(object theirMaybeNumber) 
{ 
    return Microsoft.VisualBasic.Information.IsNumeric(theirMaybeNumber); 
} 

I risultati non sono tutti soldi, il che è piuttosto confuso per me.

+0

Che cos'è 'myIsMaybeMoneyValidator'? Dove è definito? – Oded

+0

In attesa di una soluzione regex ... Anche perché non stai usando regex Scommetto che ci sono molti esempi molto buoni di loro per USD. –

+0

Hai bisogno di imparare le espressioni regolari – JoelFan

risposta

11

Sembra che ci sia una convalida integrata per soldi in C#.

float num; 
bool isValid = float.TryParse(str, 
NumberStyles.Currency, 
CultureInfo.GetCultureInfo("en-US"), // cached 
out num); 

DA: https://stackoverflow.com/a/617847/290822

+0

Questo ha funzionato, grazie. –

1

Prova decimal.TryParse (myTest4, NumberStyles.Currency, new CultureInfo ("en-US"), fuori myResult4)

0

Basta usare:

float value; 
bool isMoney= float.TryParse(str, 
    NumberStyles.Currency, 
    CultureInfo.GetCultureInfo("en-US"), 
    out value); 

Se vuoi sapere che cosa c'è che non va nel tuo codice, probabilmente è Microsoft.VisualBasic.Information. IsNumeric (theirMaybeNumber); Non si gestiscono le virgole.

+1

hmm duplicazione della mia risposta ... ad eccezione del browbeating dell'OP .. –

+1

IsNumeric restituisce anche True se Expression contiene un'espressione numerica valida che inizia con un carattere + o - o contiene virgole. –