Ich habe versucht, die Antwort zu machen, die auf zwei Dezimalstellen gedruckt wird. Die gesamte Mathematik muss bei diesem Format von zwei Dezimalstellen bleiben. Ich habe ein paar Dinge ausprobiert und bin mir nicht sicher, was ich ändern soll, damit das funktioniert.Formatieren von einem Doppel auf zwei Dezimalstellen
double pdt1 = 239.99;
double pdt1Total;
double pdt2 = 129.75;
double pdt2Total;
double pdt3 = 99.95;
double pdt3Total;
double pdt4 = 350.89;
double pdt4Total;
double wage = 200;
double percentage = 9;
double total;
double answer;
double i = 100;
double a;
double b;
double c;
double d;
Console.Write("Enter number sold of product #1: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #2: ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #3: ");
c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #4: ");
d = Convert.ToInt32(Console.ReadLine());
pdt1Total = a * pdt1;
pdt2Total = b * pdt2;
pdt3Total = c * pdt3;
pdt4Total = d * pdt4;
total = (pdt1Total + pdt2Total + pdt3Total + pdt4Total);
string.Format("{0:0.00}", total);
string.Format("{0:0.00}", answer = (total * percentage/i) + wage);
Console.WriteLine("Earnings this week: "+answer+"");
Was nicht funktioniert? – Stobor
Sie möchten wahrscheinlich nicht Gleitkomma für Währung verwenden - die Rundungsfehler beißen Sie schließlich. Es scheint nichts eingebaut zu sein, aber Links wie http://www.codeproject.com/Articles/28244/A-Money-type-for-the-CLR zur Inspiration. Mit dem gesagt, wenn Sie Dinge wie Prozentberechnungen zu tun beginnen, können Sie nicht wirklich vermeiden, ein paar mehr Dezimalstellen hinzuzufügen ... – canton7
Ich brauche die Doppel, um immer auf zwei Dezimalstellen zu bleiben, aber die Antwort wird dies nicht widerspiegeln. also ein Format von: 209.00 (wenn Sie ein Produkt für Produkt drei und Null für alle anderen gewählt haben, sollte dies die Antwort sein.) –