different outputs from a simple string format call

Viewed 35

On my windows machine running Visual Studio 2019, when I convert a decimal to a string:

decimal n = 200;
var s = n.ToString("P0");

... I get: 200%. (no space)

However when I do this on my Mac in VsCode, I get 200 % (with a space).

This is causing some unit tests to fail. What's the best way to get consistent behavior for the formatting?

1 Answers

When number gets converted into string, the platform in your case takes the current system culture into consideration. To make it consistent you may use CultureInfo.InvariantCulture

decimal number = 2;
var result = number.ToString("P0", CultureInfo.InvariantCulture);
Related