Formato decimal localizado y formateado

The example explains the question

esperado : 17,590

Primer intento:

const decimal value = 17.59m;
const string format = "{0:0,000}";
var result = string.Format(format, value);

Esto resultará 0,018 of course because the culture is en-US and , is interpreted as thousand separator.

Segundo intento:

const decimal value = 17.59m;
var result = Convert.ToString(value, CultureInfo.GetCultureInfo("pt-BR"));

Ahora tengo 17,59

How can I merge format and localization using asp.net framework features?

preguntado el 30 de enero de 12 a las 19:01

You are not doing any parsing here (that is getting a decimal from a string), but formato getting a string representation of a decimal. -

1 Respuestas

Puedes llamar ToString directamente en el decimal value - it has overloads that take format strings and a CultureInfo objeto.

Note that in a numeric format string the decimal separator is represented as a . (which will get converted to the appropriate decimal separator).

Esta:

var result = 17.59m.ToString("0.000", CultureInfo.GetCultureInfo("pt-BR"));

Produce:

17,590

Respondido el 31 de enero de 12 a las 00:01

Thanks, the optional was hiding the ToString with parameters. - Custodio

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.