Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instead of currency symbol I get a question mark into the Command Prompt

Tags:

c#

linq

I use Windows 7, Visual Studio 2013, C# and .NET 4.5.

My problem is the output of the line below :

 Console.WriteLine("Car`s value: {0:C} ", myNewCar.determineMarketValue());

myNewCar.determineMarketValue() returns a double.

How can I fix this problem?

My output is this: qmarkc#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson15SimpleClasses
{
    class Program
    {

        static void Main(string[] args)
        {
            Car myNewCar = new Car();
            myNewCar.Make = "Oldsmobile";
            myNewCar.Model = "Cutlas Supreme";
            myNewCar.Year = 1986;
            myNewCar.Color = "Silver";

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

                Console.WriteLine("{0} - {1} - {2}",
                myNewCar.Make,
                myNewCar.Model,
                myNewCar.Color);


            Console.WriteLine("Car`s value: {0:C} ", myNewCar.determineMarketValue());

            Console.ReadLine();
        }

    }

    class Car
    {

        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

        public double determineMarketValue()
        {
            double carValue = 100.0;

            if (this.Year > 1990)
                carValue = 10000.0;
            else
                carValue = 2000.0;

            return (carValue);
        }

    }


}

I added my code ..so simple yet doent work :(

Update: Code updated to use Console.OutputEncoding = System.Text.Encoding.Unicode; and also my currency and console setting are shown below: enter image description here

The problem as you can see is that even though i updated my code to use unicode changed my cmd settings to use Lucida Console font when i execute the program from VS the font remains the same Raster fonts option.

LAST EDIT:Here is how to change the console font used by Visual Studio console fast and simple.Now currency appears correctly in my program : Control console font & layout used by C# .NET console application

like image 549
strax Avatar asked Mar 16 '15 12:03

strax


3 Answers

Add

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

before writing output.

You should also ensure the console font is TrueType.

like image 127
Alex K. Avatar answered Oct 22 '22 09:10

Alex K.


It is by design.

.NET console application outputs text using some predefined system font (usually Lucida Console, but it can be Consolas or other similar font).

That font not necessary has symbol for your currency, so that symbol can be displayed incorrectly. See this link for supported currencies symbols in Lucida Console.

You can't easily fix it in console application just because it is not so easy to change font used for displaying text in console (it is possible with some WinAPI calls, I suppose).

like image 43
Andrey Korneyev Avatar answered Oct 22 '22 09:10

Andrey Korneyev


Just add:

using System.Text;
Console.OutputEncoding = Encoding.Default;

Then you can use your keyboard € symbol... it works for me! ;)

like image 25
Alexandre Alves Avatar answered Oct 22 '22 10:10

Alexandre Alves