【发布时间】:2014-01-14 13:34:36
【问题描述】:
我正在创建一个人注册为控制台应用程序,并且我正在使用几种不同的数据类型,我尝试将它们传递给一个字符串。
除十进制外,一切似乎都转换得很好。当我从构造函数创建对象时,十进制“平均收入”不会显示我写的数字。为什么会发生这种情况?是否有简单的解决方案?
我猜我需要以某种方式转换值..
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();
}
}
【问题讨论】:
-
格式化绝对是你的问题...
-
"... 收入:{8}" 也许?
-
哈哈,这很尴尬,忘记了 8.. 对不起,伙计们
标签: c# string parsing decimal formatting