maverick maverick - 1 month ago 20
C# Question

Unicode CultureInfo.NegativeSign shown as ? in Console

I am trying to learn about NumberFormatInfo.NegativeSign Property, and put in the following code in VS.

CultureInfo ci = CultureInfo.CreateSpecificCulture ("");
ci.NumberFormat.NegativeSign = "\u203E";
double[] numbers = { -1.0, -16.3, -106.35 };

foreach (var number in numbers)
Console.WriteLine (number.ToString (culture));


Since I got a number of errors, I modified it to...

System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture ("");
ci.NumberFormat.NegativeSign = "\u203E";
double[] numbers = { -1.0, -16.3, -106.35 };

foreach (var number in numbers)
Console.WriteLine (number.ToString (ci));


And it compiled fine. But the output was:

?1
?16.3
?106.35


I got the code from .Net library framework definitions on Microsoft page here and the Unicode was supposed to change the negative sign to the OVERLINE character. Did I do anything wrong? Why am I getting the "?" sign instead?

I did not know about the console output configuration requirement. Thanks. One other item; the original code used culture instead of ci in the last line. I assumed that was a typo and changed it. I hope I was not wrong. The following are the lines they have:

CultureInfo ci = CultureInfo.CreateSpecificCulture ("");
ci.NumberFormat.NegativeSign = "\u203E";
double[] numbers = { -1.0, -16.3, -106.35 };

foreach (var number in numbers)
Console.WriteLine (number.ToString (culture));

Answer

The console can by default not display unicode characters. You need to specifically enable that using the following line before your own code:

Console.OutputEncoding = System.Text.Encoding.Unicode;
Comments