2014-01-14 29 views
1

我創建一個人註冊爲控制檯應用程序,我使用幾個不同的數據類型至極,我嘗試傳遞給一個字符串。字符串格式,不能顯示小數

似乎一切都好轉exept小數。當我從構造函數中創建對象時,小數點「平均收入」不會顯示我編寫的數字。爲什麼會發生這種情況,並有一個簡單的解決方案呢?

我猜我需要的值轉換以某種方式..

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(); 
    } 
} 
+1

格式,絕對是你的問題...... –

+4

」 ......收入:{8}「也許? – Plue

+0

哈哈,這是尷尬,忘了8 ..對不起,夥計們 – koffe14

回答

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); 
相關問題