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();   

        }
    }
}
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • 4
    You shouldn't be using `double` for monetary values - it's too prone to rounding issues. Use `decimal` instead – marc_s Sep 23 '12 at 16:09
  • Don't use `double` for money values, use [`Decimal`](http://msdn.microsoft.com/en-us/library/364x0z75.aspx) instead! Also read: http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when . – Styxxy Sep 23 '12 at 16:10
  • This is a dup, see this post: http://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-instead-of-3-are-you-kidding-me – Darroll Sep 23 '12 at 16:11

1 Answers1

0

I recommend using Decimal, but to answer your question in case you are struggling with just patching some ugly legacy code you could use something like:

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;
}

then call:

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

...happy coding.

Dave
  • 1,823
  • 2
  • 16
  • 26