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:
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:
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
Add
Console.OutputEncoding = System.Text.Encoding.Unicode;
before writing output.
You should also ensure the console font is TrueType.
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).
Just add:
using System.Text;
Console.OutputEncoding = Encoding.Default;
Then you can use your keyboard € symbol... it works for me! ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With