Search code examples
c#unit-testingformattingtostringcultureinfo

Why is 1 000 000 not equal to 1000000.toString("N", new CultureInfo("fr-FR"))


I writing some unit tests around string conversions and currency.

I'm testing that the string is returned in the format that I expect it, given the CultureInfo the method is passed.

 public string GetOverspendAmount(double _amount, CultureInfo cultureInfo)
 {
    return amount.ToString("N", cultureInfo);
 }

I'm unit testing this bit of code with the following call -

double amount = 1000000;
CultureInfo ci = new CultureInfo("fr-FR", false);

GetOverspendAmount(amount, ci).Should().Be("1 000 000");

but the test fails. The expected and actual look identical but when put into an ascii converter the following results are shown

Expected - 1 000 000
49 32 48 48 48 32 48 48 48

Actual - 1 000 000
049 194 160 048 048 048 194 160 048 048 048

They're not normal spaces, but I'd like to know what they are, and why they aren't just normal spaces?


Solution

  • The space is a no-break space (UTF-8 194 160) and not the regular space (UTF-8 32).

    https://utf8-chartable.de/unicode-utf8-table.pl?utf8=dec

    The no-break space makes a difference in formats like HTML, as it wouldn't allow automatic line break after it. It also doesn't allow multiple spaces collapsing into a single one.