Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Dollars and Percentages in C#

Posted on 2012-09-05
Medium Priority
336 Views
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);
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;
}
else
{
}
}
foreach (KeyValuePair<String, double> total in totals)
{
Console.WriteLine("Account {0}, amount = {1}", total.Key, total.Value);
}
}
}
``````
0
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.
0

Author Closing Comment

ID: 38374639
Thank you! :)
0

## Featured Post

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
Course of the Month21 days, 6 hours left to enroll