System.Globalization.NumberFormatInfo Convertir String a Decimal

Convierte un valor string a su equivalente decimal utilizando System.Globalization.NumberFormatInfo

/// <summary>

/// Convertir un valor en formato String a su equivalente Decimal

/// </summary>

/// <param name="strValor">Valor numérico en formato cadena</param>

/// <param name="SeparadorMiles">Carácter de miles o separador de grupo a utilizar</param>

/// <param name="SeparadorDecimal">Separador de decimales a utilizar</param>

/// <param name="NoDecimales">Número de decimales a utilizar</param>

/// <returns>Equivalente decimal de la cadena enviada</returns>

public Decimal StringADecimal(string Valor, char SeparadorMiles, char SeparadorDecimal, int NoDecimales)

{

System.Globalization.NumberFormatInfo nfiFormato;

nfiFormato = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

try

{

nfiFormato.NumberGroupSeparator = SeparadorMiles.ToString();

nfiFormato.NumberDecimalSeparator = SeparadorDecimal.ToString();

nfiFormato.NumberDecimalDigits = NoDecimales;

return Convert.ToDecimal(Valor, nfiFormato);

}

catch (Exception e)

{

throw e;

}

}

About omaracostacasas

ING SOFTWARE
This entry was posted in Microsoft .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s