Pregunta

i've seen the question Odd decimal type behavior for ToString(IFormatProvider)

this is my test code

// Define an array of numbers to display.
double[] numbers = { -1.5345e16, -123.4321, 19092.123, 1.1734231911290e16 };
// Define the culture names used to display them.
string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" };

foreach (double number in numbers)
{
    Console.WriteLine("{0}:", Convert.ToString(number,
                              System.Globalization.CultureInfo.InvariantCulture));
    foreach (string cultureName in cultureNames)
    {
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
        culture.NumberFormat.NumberGroupSizes = new int[] {2,2,3};
        culture.NumberFormat.NumberDecimalDigits = 5;
        Console.WriteLine(".ToString          {0}: {1,20}",
                          culture.Name, number.ToString("N", culture));
        Console.WriteLine("Convert.ToString   {0}: {1,20}", 
            culture.Name, Convert.ToString(number, culture));
    }
    Console.WriteLine();
}

and the sample result is as below:

why is Convert.ToString not applying the format?

enter image description here

This is with Win7 Prof/VS2010+SP1Rel (10.0.40219.1)

¿Fue útil?

Solución

As per the documentation for Convert.ToString(double, IFormatProvider):

This implementation is identical to Double.ToString(IFormatProvider)

... which is then documented as:

The ToString(IFormatProvider) method formats a Double value in the default ("G", or general) format of a specified culture.

When you call ToString, you're passing in N - if you pass in G instead, you'll get the same results as Convert.ToString.

The G format specifier documentation states:

The result string is affected by the formatting information of the current NumberFormatInfo object. The following table lists the NumberFormatInfo properties that control the formatting of the result string.

... and lists NegativeSign, NumberDecimalSeparator, PositiveSign. Your code doesn't set any of those properties, which is why it's not making a difference. If you write:

culture.NumberFormat.NumberDecimalSeparator = "#"

for example, then that will affect the output of Convert.ToString.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top