【发布时间】:2015-03-16 12:54:53
【问题描述】:
我使用 Windows 7、Visual Studio 2013、C# 和 .NET 4.5。
我的问题是下面一行的输出:
Console.WriteLine("Car`s value: {0:C} ", myNewCar.determineMarketValue());
myNewCar.determineMarketValue() 返回一个双精度值。
我该如何解决这个问题?
我的输出是这样的:
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);
}
}
}
我添加了我的代码 .. 如此简单但没用 :(
更新:代码更新为使用Console.OutputEncoding = System.Text.Encoding.Unicode;
我的货币和控制台设置如下所示:
您可以看到的问题是,即使我将代码更新为使用 unicode,但当我从 VS 执行程序时,我的 cmd 设置更改为使用 Lucida Console 字体,但字体仍然是相同的 Raster 字体选项。
最后编辑:这里是如何快速简单地更改 Visual Studio 控制台使用的控制台字体。现在货币正确显示在我的程序中:Control console font & layout used by C# .NET console application
【问题讨论】:
-
这将是一个编码问题...确保您没有使用 ASCII 并检查 Unicode / UTF 等。
-
你能展示一下你得到了什么以及
myNewCar.determineMarketValue()的价值吗? -
另外:您目前的文化是什么?例如开始->系统设置中的语言设置是什么?那里的货币设置是什么?
-
@strax 您将根据文化获得不同的输出。更多信息请看这里msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
-
在哪里可以检查我的项目编码设置?