4

I am working on strings in .Net Core. I have a string formatted using :n and when it is formatted the output is 123 456,00. I wanted to assert that the formatted string is equal to the string i wish it to be but i get an Assert.Equal Failure() and the problem is in the space character. In the output it asserts that the two spaces are different.

Here is my code :

public void Separator()
{
    var str = string.Format("{0:n}", 123456);
    Assert.Equal("123 456,00",str);
}

I also compared the space character from the formatted string to a regular space character with an assert as follows Assert.Equal(' ',str[3]);i get that the expected value is 0x00a.

Why is this happening and how can i get the same character without using string.Format()?

6
  • Do you want to say that Assert.Equal() says 123 456,00 is not equal to 123 456,00? Then you need to check the exact values in some hex editor. Or just use some online tool to see if there is no typo. Try r12a.github.io/apps/conversion Commented Aug 22, 2017 at 11:17
  • yes i get a difference in (pos 3) meaning the two spaces are not equal. Commented Aug 22, 2017 at 11:18
  • Are you sure the spaces are identical? Check at r12a.github.io/apps/conversion Commented Aug 22, 2017 at 11:18
  • Which culture are you using here? Commented Aug 22, 2017 at 11:20
  • they are not identical that is my question, how can i print the same space without using string.Format() ? Commented Aug 22, 2017 at 11:21

1 Answer 1

4

The culture you are using specifies that the number group separator is a different ASCII character than space. I'm guessing you are using ru-RU here, which means the digit is ASCII character 160, which means those strings will not match if you have just typed a space.

You could replace the space with the culture's separator like this for example:

var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
var stringToCompare = "123 456,00".Replace(
    " ", 
    currentCulture.NumberFormat.NumberGroupSeparator);

Assert.Equal(stringToCompare, str);
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, a NO-BREAK SPACE in a number is much more appropriate than a normal space. Nit pick: Characters in .NET are Unicode, not ASCII. NO-BREAK SPACE has one UTF-16 code unit, with the value '\u00a0' (160).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.