我创建了一个人注册为控制台应用程序,并且我尝试使用几种不同的数据类型来尝试传递给字符串。
似乎所有东西都被转换为精细的小数点。小数"平均收入"当我从构造函数中创建对象时,我不会显示我写的数字。为什么会发生这种情况并且有一个简单的解决方案呢?
我猜我需要以某种方式转换价值..
public class PersonRegistry : IPersonRegistry
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int YearOfBirth { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Occupation { get; set; }
public decimal AvarageIncome { get; set; }
public PersonRegistry(string Fname, string Lname, int year, string country, string city,
string state, int zip, string occupation, decimal income)
{
FirstName = Fname;
LastName = Lname;
YearOfBirth = year;
Country = country;
City = city;
State = state;
Zip = zip;
Occupation= occupation;
AvarageIncome = income;
}
public override string ToString()
{
return string.Format("* First name: {0}\n* Last name: {1}\n* Born: {2}\n* Country: {3}\n* State: {4}\n* Oklahoma: {5}\n* Zip code: {6}\n* Occupation: {7:C}\n* Income: ",
FirstName, LastName, YearOfBirth, Country, City, State, Zip, Occupation, AvarageIncome);
}
}
static void Main(string[] args)
{
PersonRegistry p1 = new PersonRegistry("Chuck", "Norris", 1940, "United States", "Ryan", "Oklahoma", 73565, "Actor", 1921.39m);
PersonRegistry p2 = new PersonRegistry("Arnold", "Schwarzenegger", 1947, "Austria", "Thal", "Steiermark", 8113, "Actor, politician, bodybuilder", 3.289654m);
var PersonRegList = new List<PersonRegistry>();
PersonRegList.Add(p1);
PersonRegList.Add(p2);
foreach (var person in PersonRegList)
{
Console.WriteLine(person);
Console.ReadLine();
}
}
答案 0 :(得分:0)
这应该是:
return string.Format("* First name: {0}\n* Last name: {1}\n* Born: {2}\n* Country: {3}\n* State: {4}\n* Oklahoma: {5}\n* Zip code: {6}\n* Occupation: {7:C}\n* Income:{8} ",
FirstName, LastName, YearOfBirth, Country, City, State, Zip, Occupation, AvarageIncome);