Stacked Stacked - 1 year ago 101
C# Question

C# currency format not working

I've a console application with the following:

var value = 999999;
System.Console.WriteLine("Price {0:c}", value);

// Prints: Price ? 999.999,00
// Expecting: Price € 999.999,00

The line below prints Price $ 999.999,00

System.Console.WriteLine("Price {0}", value.ToString("c", new CultureInfo("en-US")));

But this line:

System.Console.WriteLine("Price {0}", value.ToString("c", new CultureInfo("fr-BE")));

Prints ? instead of

What's wrong with the code?

Answer Source

There is nothing wrong in the code assuming your default culture is supposed to print euro. However you seems to be running the program in a console window either directly or through VS launch. The default code page that is used is 437 that can't display the euro symbol. You have to change it to one that can, for example 1252.

Active code page: 437

C:\>chcp 1252
Active code page: 1252

€ 9.999,00

Now if you run your program you should see something different. If you are using the default font (Raster Fonts) it would still not display the euro symbol correctly. Change it to Lucida Console and the program should work as expected.

Command Prompt  Menu Command Prompt Properties

If you want to control this from the program, add appropriate encoding

//Console.OutputEncoding = Encoding.Default;
//Console.OutputEncoding = Encoding.UTF8;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download