Dollars and Percentages in C#

Posted on 2012-09-05
Last Modified: 2012-09-06
Ok Experts I have been playing with this code, trying several scenarios and can not figure out how to display the totals in $$ and to calculate the total by 10% ( the total being 10% of the total.Value)

static void Main(string[] args)
            List<string> accountNames = new List<string>();

            // create a list of deposits           
            List<KeyValuePair<string, double>> deposits = new List<KeyValuePair<string, double>>();            
            foreach (string accountName in accountNames)
                string line;                
                while (true)
                    Console.WriteLine(" Please enter new amount for the Account {0} ", accountName);
                    line = Console.ReadLine();
                    if (line.StartsWith("Z", StringComparison.CurrentCultureIgnoreCase)) break;                                        
                    Double amount = Convert.ToDouble(line);
                    KeyValuePair<string,double> deposit = new KeyValuePair<string,double>(accountName,amount);

            Dictionary<string, double> totals = new Dictionary<string, double>();
            foreach (KeyValuePair<string, double> item in deposits)
                if (totals.ContainsKey(item.Key))
                    totals[item.Key] += item.Value;                 
            foreach (KeyValuePair<String, double> total in totals)
                Console.WriteLine("Account {0}, amount = {1}", total.Key, total.Value);

Open in new window

Question by:December2000
    LVL 16

    Accepted Solution

    If I understand your question, you're looking for string formatting patterns. Look up string.format. There are many examples. It's something that I always find I have to look up on demand because of my lousy memory.

                double total = 1236.45;
                double tenPercent = total * ((double)10 / (double)100);

                Console.WriteLine("Total is {0}", total.ToString("C2"));
                Console.WriteLine("Ten Percent of total is {0}", tenPercent.ToString("C2"));

    This example formats a double using a built in "currency" formatter.

    Am I misunderstanding what you're looking to do to calculate a percentage? Because it's pretty straightforward, as shown above.

    Author Closing Comment

    Thank you! :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now