andersnm / ExcelNumberFormat

Parse and render Excel number format strings
MIT License
79 stars 27 forks source link

Thousand separator does not respect the provided CultureInfo #14

Closed Riboe closed 5 years ago

Riboe commented 5 years ago

When formatting numbers, the culture is ignored in regards to the thousand separator character(s).
This appears to be because the thousand separator is hard coded as a comma.

As an example, the following code produces an incorrect result:

var format = new NumberFormat("#,##0");
var culture = new CultureInfo("da-DK");
var value = 1234567890d;
var result = format.Format(value, culture);
// result is "1,234,567,890", when it should be "1.234.567.890"

I believe the fix is to update the FormatThousandSeparator function inside Formatter.cs to the following:

static void FormatThousandSeparator(string valueString, int digit, CultureInfo culture, StringBuilder result)
{
    var positionInTens = valueString.Length - 1 - digit;
    if (positionInTens > 0 && (positionInTens % 3) == 0)
    {
        // Remove the below line:
        //result.Append(",");

        // Add this line instead:
        result.Append(culture.NumberFormat.NumberGroupSeparator);
    }
}
andersnm commented 5 years ago

Takk for nice bug report!

Riboe commented 5 years ago

You're welcome :)