Having trouble getting and output to be in currency format

Locked Having trouble getting and output to be in currency format

  • Saturday, July 21, 2012 5:47 PM
     
     

    I am having difficulty getting an output I need to be in currency format.  I have a class that has a method in it that calculates weekly pay.  Here is the code for my class method:

    public double CalculateWeeklyPay()
            {
                double weeklyPay;

                weeklyPay = this.annualSalary / 52;
                
                return weeklyPay;
            }

    I have a To.String method in the same class that I am using in the main program.  I cannot figure out how to get the output for weeklyPay to be formatted for currency.  Here is my code for the To.String method:

    public override string ToString()
            {
                string output;

                output = "============ Employee Information ============"+ "\n";
                output += "Name: " + firstName + " " + lastName +"\n";
                output += "Gender: " + gender +"\n";
                output += "Dependents: " + dependents +"\n";
                output += "Annual Salary: " + annualSalary.ToString("C2") + "\n";
                output += "Weekly Pay:  " + CalculateWeeklyPay();
                return output;
            }

    Here is the code I have for getting the To.String method:

    Console.WriteLine(newEmployee.ToString());

    With what I have here, the program compiles and runs but does not show the weekly pay in currency.  I have tried numerous lines of code to fix this, but I keep getting errors.  I am stuck.  If anyone can point me in the right direction I would be greatfull. Thanks.

All Replies

  • Saturday, July 21, 2012 6:19 PM
     
     

    Check this 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

        class Program
        {
            static void Main(string[] args)
            {
                String ans=Calculate();

                Console.WriteLine(ans);
                Console.ReadLine();
            }
            public static double CalculateWeeklyPay(double annualsalary)
            {
                double weeklyPay;

                weeklyPay = annualsalary / 52;

                return weeklyPay;
            }
            public static string Calculate()
            {
                string output="";

                output += "Weekly Pay:  " + CalculateWeeklyPay(1000.00);
                return output;
            }






        }

  • Saturday, July 21, 2012 6:21 PM
     
     Answered
    Hi darkavarice;

    Change this line:

    output += "Weekly Pay:  " + CalculateWeeklyPay();

    To this :

    output += "Weekly Pay:  " + CalculateWeeklyPay().ToString("C");

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed As Answer by BonnieBMVP Sunday, July 22, 2012 3:48 PM
    • Marked As Answer by darkavarice Sunday, July 22, 2012 11:41 PM
    •  
  • Saturday, July 21, 2012 7:33 PM
     
      Has Code

    I would do it this way:

    double d = 1.3334346;
    Console.WriteLine("d = {0:c2}", d); //$1.33
    d = d + .555;
    Console.WriteLine("d = {0:c2}", d); //$1.89 (rounded up)
    
    //to save as string
    string dollarValue = String.Format("{0:c2}", d);
    Console.WriteLine(dollarValue);
    See this page for more info on String Formatting in .NET: http://blog.stevex.net/string-formatting-in-csharp/


    Dan Randolph - My Code Samples List

  • Tuesday, July 24, 2012 8:13 PM
     
     
    Thanks to those who posted a reply.  All of your ideas were helpful.  Fernando, your example was what I needed.  I was basically trying to do the same thing but was getting the syntax wrong, so thank you.  Thanks again everyone.
  • Tuesday, July 24, 2012 8:28 PM
     
     
     

    Not a problem, glad I was able to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Saturday, July 28, 2012 7:20 PM
     
     

    Hello.  I am a student taking a C# class and I am stuck on a part of this week's lab.  Here is what it is asking me to do:

    Create the overloaded CalculateWeeklyPay method that accepts a double "modifiedSalary" argument. The method shall update the annualSalary attribute (use the AnnualSalary property to ensure the value is valid), and then return the updated weekly pay based on the new annual salary value.

    After that I am to do this:

    Prompt the user to provide an updated annual salary for employee1, retrieve the value and invoke the overloaded CalculateWeeklyPay, and then display only the updated weekly pay.

    This lab is building off of a lab I did last week.  Here is the overloaded  CalculateWeeklyPay method I created.  I am not sure if I created it properly:

     public double CalculateWeeklyPay(double modifiedSalary)
            {
                double weeklyPay=0;

                weeklyPay = modifiedSalary / 52;
                return weeklyPay;
            }

    I am having difficulty invoking this method in the main body of my program.  I found a way to acomplish what getting the updated weeklyPay but not the way the lab is asking.  This is what I came up with:

     newEmployee.AnnualSalary=InputUtilities.getDoubleInputValue("Enter the updated annual salary:");
                Console.WriteLine(newEmployee.FirstName + " " + newEmployee.LastName + "'s updated weekly pay: {0:C}",newEmployee.CalculateWeeklyPay());

    This gets and displays the updated info, but not the way the lab is asking.  Any advice on how to accomplish this the correct way would be geatly appreciated.  Thanks.

  • Saturday, July 28, 2012 10:11 PM
     
     

    Hi darkavarice;

    Seeming you are a student we can't write code for you but we can answer question you may not be understanding in your project. If you post your code that you are working with and ask a question so that we may lead you in the right direction we can do that. You can post your code using the code tool which can be selected using the second icon from the right on the post tool bar and select C# as the programming language from the drop down. Or if it is a large amount of code you can zip it up and upload it to a web site such as Microsoft Skydrive in a public folder and post the link here.

    Also please do not duplicate post you have posted four post with the same question please go back to the other three Post and delete the questions.

    Thank you.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Monday, July 30, 2012 10:36 AM
    Moderator
     
     

    Hi darkavarice ,

    I am sorry for merging  this two post together  by mistake . If you still have this question ,would you please start a new topic about this latest problem .

    Thanks for your understanding and sorry again .

    Reagards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us