Benutzer mit den meisten Antworten
I tried to make some code that will determine the percentage of 2 numbers you type in yourself. So my problem is if you run it it will only display the numbers corrctly under 10% above it will round them heavily.

Frage
-
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Prozent { class Prozent { static float Percentage(int x, int y) { float z; if (x > y) { z = x / y; z = 100 / z; return z; } else { z = y / x; z = 100 / z; return z; } } static void Main(string[] args) { while (true) { Console.WriteLine("Write the 2 numbers you want the percentage of, one line each"); Console.WriteLine("type stop to quit"); string g = Console.ReadLine(); if (g == "stop") break; else { int x = Convert.ToInt32(g); int y = Convert.ToInt32(Console.ReadLine()); float f = Percentage(x, y); if(x > y) Console.WriteLine(y + " is " + f + " percent of " + x); else Console.WriteLine(x + " is " + f + " percent of " + y); } } } } }
- Bearbeitet DaFatPoro Samstag, 7. Oktober 2017 15:59
- Bearbeitet Stefan FalzModerator Sonntag, 8. Oktober 2017 08:19 Codeformatierung hinzugefügt.
Antworten
-
Hallo,
wenn du deine floats durch was auch immer ersetzt, wird das nicht direkt weiterhelfen. Wirf mal einen Blick auf die Signatur deiner Methode:
static float Percentage(int x, int y)
Da solltest du also mal den Type deiner Parameter überdenken. Weil dein Rückgabetyp ein float ist, ändert das erstmal nichts daran, dass du 2 Integers dividierst. Ob du dieses Resultat dann als float oder decimal oder sonst was zurückgibst, ist nicht mehr so entscheidend.
So könnte es beispielsweise funktionieren:
public decimal Percentage(decimal z, decimal n) { if ( n == 0 ) { throw new ArgumentException("..."); } return z / n; } public decimal Percentage(int z, int n) { if ( n == 0 ) { throw new ArgumentException("..."); } return (decimal)z / (decimal)n; }
Gruß
- Bearbeitet K. Pater Sonntag, 8. Oktober 2017 10:13
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Donnerstag, 12. Oktober 2017 14:01
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 27. Oktober 2017 13:19
Alle Antworten
-
Hi,
poste bitte auf Deutsch und stell deine Frage bitte in den Inhaltsbereich und nicht einfach so in Betreff.
So ganz allgemein: float ist für dich wohl der falsche Datentyp. Siehe dazu:
https://exceptionnotfound.net/decimal-vs-double-and-other-tips-about-number-types-in-net/
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
- Bearbeitet Stefan FalzModerator Samstag, 7. Oktober 2017 16:51
-
Hi,
das wäre eine Möglichkeit. Ob das nun dein Problem löst, kann ich dir aber eher sagen, wenn Du deine Frage mal auf Deutsch und ein wenig ausführlicher beschreibst.
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community -
Hallo,
wenn du deine floats durch was auch immer ersetzt, wird das nicht direkt weiterhelfen. Wirf mal einen Blick auf die Signatur deiner Methode:
static float Percentage(int x, int y)
Da solltest du also mal den Type deiner Parameter überdenken. Weil dein Rückgabetyp ein float ist, ändert das erstmal nichts daran, dass du 2 Integers dividierst. Ob du dieses Resultat dann als float oder decimal oder sonst was zurückgibst, ist nicht mehr so entscheidend.
So könnte es beispielsweise funktionieren:
public decimal Percentage(decimal z, decimal n) { if ( n == 0 ) { throw new ArgumentException("..."); } return z / n; } public decimal Percentage(int z, int n) { if ( n == 0 ) { throw new ArgumentException("..."); } return (decimal)z / (decimal)n; }
Gruß
- Bearbeitet K. Pater Sonntag, 8. Oktober 2017 10:13
- Als Antwort vorgeschlagen Dimitar DenkovMicrosoft contingent staff, Administrator Donnerstag, 12. Oktober 2017 14:01
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Administrator Freitag, 27. Oktober 2017 13:19