0

I'm starting out trying to learn code, using visual studio and a c# book. It doesn't go over how to change a double to a dollar amount. The following is the program I have so far (an assignment). How can I either change the format of my output (WITHOUT adding using system.globalization;) Or make it where the output will always read a 1.1 as $1.10 and 1.5678 as $1.57. For example if I were to put that I work 39 hours at 10.50 an hour.

    {
        //Declare all variables:
        double Hours;
        double Rate;
        double GrossPay;
        double FederalTax;
        double StateTax;
        double SocialSecurity;
        double NetPay;
        double TotalTax;

        //Introduction:
        Console.WriteLine("Welcome to the Pay Calculator!!!");

        //User input of hours:
        Console.WriteLine("Please enter the hours you worked this pay period: ");
        Hours = Convert.ToDouble(Console.ReadLine());

        //User input for rate:
        Console.WriteLine("Please enter your rate of pay: ");
        Rate = Convert.ToDouble(Console.ReadLine());

        //Calculations
        GrossPay = Hours * Rate;
        FederalTax = GrossPay * 0.20d;
        StateTax = GrossPay * 0.05d;
        SocialSecurity = GrossPay * 0.062d;
        NetPay = GrossPay - FederalTax - StateTax - SocialSecurity;
        TotalTax = FederalTax + StateTax + SocialSecurity;

        //OutPut
        Console.WriteLine($"Your gross pay is: ${GrossPay}");
        Console.WriteLine($"Your federal tax obligation is: ${FederalTax}");
        Console.WriteLine($"Your state tax obligation is: ${StateTax}");
        Console.WriteLine($"Your social security obligation is: ${SocialSecurity}");
        Console.WriteLine($"Your total tax obligation is: $ {TotalTax}");
        Console.WriteLine($"This will bring your total net pay to: ${NetPay}");

        Console.ReadKey();
    }
ArcInfinity
  • 1
  • 1
  • 2
  • You say "decimal" but declared a "float", can you clarify what you really mean here? You do know there is a different type called `decimal` right? – Lasse V. Karlsen Jun 21 '16 at 06:57
  • It should cover how to format a decimal number, the rest is just adding the dollar sign in front. Probably using `'$'0.00` as format string would do it. – Sami Kuhmonen Jun 21 '16 at 06:57
  • 1
    If you want to work with currency, it would be better to use `decimal` rather than `float` to start with. – Damien_The_Unbeliever Jun 21 '16 at 06:57

3 Answers3

2

You should use ToString("C") if you want to format as a currency value. First add the following line to your using statements:

using System.Globalization;

Then:

Console.WriteLine(FavoriteNumber.ToString("C", CultureInfo.GetCultureInfo("en-US")));

Result:

$11.88
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

it is so simple just use string.format function to give a custom format to string

example:

decimal[] amounts = { 16305.32m, 18794.16m };

Console.WriteLine("   Beginning Balance           Ending Balance");

Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);

this code will display this output:

// Displays:

//        Beginning Balance           Ending Balance

//        $16,305.32                      $18,794.16    
mostafa
  • 261
  • 1
  • 2
  • 10
0

ToString with following format specifier ("C", en-US). 123.456 ("C", en-US) -> $123.46 More info: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

vipasane
  • 355
  • 2
  • 10