2012-09-23 77 views
1
namespace Assignment02 
{ 
    class assigement02question04 
    { 
     public static void answer() 
     { 
      //declare variables: ID number = int, rate of pay, number of hours , overtime , gross pay = double 
      //disply what is your employee ID number 
      //catpure ID number 
      //disply what is your rate of pay 
      //catpure rate of pay 
      //disply what is the number of hours you have worked 
      //catpure number of hours worked 
      //display what is the amount of over time hours 
      //catpure over time hours 
      //Calculate gross pay = (number of hours + over time x 1.5) x rate of pay 
      //calculate net pay = gross pay - gross pay x 0.33 - 25 (convert to currency) 
      //disply ID number , number of hours , rate of pay , over time hours and gross pay 




      //declare variables 

      int ID_Number; 
      double rate_of_pay; 
      double number_of_hours; 
      double over_time; 
      double gross_pay; 
      double net_pay; 

      //input your employee ID 
      Console.Write("What is your employee ID number? "); 
      ID_Number = int.Parse(Console.ReadLine()); 

      //input your rate of pay 
      Console.Write("What is your rate of pay? "); 
      rate_of_pay = double.Parse(Console.ReadLine()); 


      //input number of hours 
      Console.Write("What is the number of hours you have worked? "); 
      number_of_hours = double.Parse(Console.ReadLine()); 

      //input over time hours 
      Console.Write("What is the amount of over time hours? "); 
      over_time = double.Parse(Console.ReadLine()); 

      //intput of our gross pay 
      gross_pay = (number_of_hours + over_time * 1.5) * rate_of_pay; 
      //input of our net pay 
      net_pay = gross_pay - gross_pay * .33 - 25; 
      net_pay = Math.Round(net_pay, 2); 
      //Disply as end result 
      Console.WriteLine("Your employee ID number is: " + ID_Number); 
      Console.WriteLine("The number of regular hours is: " + number_of_hours); 
      Console.WriteLine("The number of O/T hours is: " + over_time); 
      Console.WriteLine("The rate of pay of is: " + rate_of_pay); 
      Console.WriteLine("Your gross pay is: $" + gross_pay); 
      Console.WriteLine("Your net pay is: $" + net_pay); 
      Console.ReadLine(); 

     } 
    } 
} 
+4

你不應該用貨幣價值的雙倍 - 這太容易導致四捨五入的問題。使用'decimal'代替 –

+0

不要使用'double'作爲貨幣值,而是使用['Decimal'](http://msdn.microsoft.com/zh-cn/library/364x0z75.aspx)!另請閱讀:http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when。 – Styxxy

+0

這是一個dup,看到這篇文章:http://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-instead-of-3-are-you-kidding-我 – Darroll

回答

0

我建議使用十進制,但要回答你的問題的情況下,你只用打補丁,你可以使用像一些醜陋的舊代碼掙扎:

public decimal ToMoney(double Input) { 
    int _precision = 2; 
    decimal _result; 
    try{ 
     double multiplier = Math.Pow(10, Convert.ToDouble(_precision)); 
     _result = System.Convert.ToDouble(Math.Ceiling(input * multiplier)/multiplier); 
    } catch {} 
    return _result; 
} 

然後調用:

string CurrencySymbol = "$"; 
Console.WriteLine("The rate of pay of is: " + CurrencySymbol + 
ToMoney(rate_of_pay).ToString()); 

...快樂的編碼。

相關問題