Tratamiento de Decimales

27/12/2004 - 11:37 por Anonimo | Informe spam
Hola grupo,

tengo este código

decimal nineBillPlus = 9876543210.9876543210M;

// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 4 and the digit
// separator to '_'.
NumberFormatInfo numInfo = culture.NumberFormat;
numInfo.NumberGroupSizes = new int[ ] { 4 };
numInfo.NumberGroupSeparator = "_";

// Use a NumberFormatInfo object for IFormatProvider.
string s1 = string.Format(
"A NumberFormatInfo object with digit
group " +
"size = 4 and digit separator " +
"= '_' is used for the IFormatProvider:" );

string s2 = string.Format( " {0,-30}{1}", "'N5' format
string:",
nineBillPlus.ToString( "N5", culture ) );


Esto me devolvería:

A NumberFormatInfo object with digit group size = 4 and
digit separator = '_' is used for the IFormatProvider:
'N5' format string: 98_7654_3210,98765

Bien, querría saber cómo realizar el proceso inverso, es
decir, si tengo una cadena

98_7654_3210,98765

cómo puedo pasara a una variable de tipo Decimal ??

Gracias y felices fiestas !!!
 

Leer las respuestas

#1 Xavi
27/12/2004 - 12:10 | Informe spam
Hola.

Utilizando el formato numérico que has definido:
NumberFormatInfo numInfo = culture.NumberFormat;
numInfo.NumberGroupSizes = new int[ ] { 4 };
numInfo.NumberGroupSeparator = "_";

puedes hacer:
string numero = "98_7654_3210,98765";
decimal d = Convert.ToDecimal(numero, numInfo);

Saludos,
Xavi.

Preguntas similares