Learn how to a build a cloud-first strategyRegister Now


Dollars and Percentages in C#

Posted on 2012-09-05
Medium Priority
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

ToddBeaulieu earned 2000 total points
ID: 38369271
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

ID: 38374639
Thank you! :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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