Autor Beitrag
vit30
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38



BeitragVerfasst: Mo 11.01.10 21:01 
Hallo!
Mit welche Funktion kann ich prüfen ob Zahl rund (ohne Komma) ist? Z.B. a = b/c.Ich mus wissen, ob ich dabei Zahl mit oder ohne Komma bekommen habe.
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Mo 11.01.10 21:15 
der operator '%' ist der 'divisionsrest'. wenn dieser null ist, bedeutet das, das die division keine zahl mir komma als ergebnis hat. gibt vielleicht besseres, is aber so das einzige was mir einfällt...
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
if (( a % b ) == 0//a und b sind die Zahlen mit denen du rechnest
{
    //Die Zahl hat kein Komma
}
else
{
    //Die Zahl hat Nachkommastellen
}
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mo 11.01.10 22:58 
Das ist eine Möglichkeit. Sie ist aber (ebenso wie fast jedes andere Verfahren) fraglich bei double-Werten, weil es dabei immer Rundungsfehler geben kann.

Eine andere Möglichkeit:
ausblenden C#-Quelltext
1:
2:
3:
double d = ...
bool isInteger = (int)d == d;   // oder
isInteger = Math.Round(d) == d;

Es handelt sich um eine ganze Zahl, wenn der ganzzahlige Anteil mit der Zahl übereinstimmt.

Gruß Jürgen
vit30 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38



BeitragVerfasst: Di 12.01.10 18:48 
Mit "%" hat es gut geklappt.
Danke.