Autor Beitrag
ethillias
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41



BeitragVerfasst: Mi 31.12.08 11:39 
Hi,

ich stelle meine Ramauslastung zur zeit in einer Gauge da, funktioniert soweit ganz gut, jetzt wollte ich eine Prozentanzeige haben, hierzu nehme ich den Progress und teile ihn durch den MaxValue das anze mal 100 sollte doch den Prozentwert ergeben, oder nicht?

ausblenden Delphi-Quelltext
1:
2:
  i:= Gauge.Progress div Gauge.MaxValue * 100;
  Label.Caption:='RAM: ' + IntToStr(i) + '%';


Bei mir steht dort dann die ganze Zeit: 'Ram: 0%'....

Danke für die Hilfe schon mal
Webo
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 577
Erhaltene Danke: 14

Win 7, Debian
C# (Visual Studio 2013), PHP, C, C++ (Eclipse, KDevelop)
BeitragVerfasst: Mi 31.12.08 12:13 
Soweit ich mich errinern kann, gibt div nur den Wert an, der vor dem Komma steht. Also 10 div 5 = 2 statt 2,0 (als Beispiel). Wenn man jetzt aber Gauge.Progress div Gauge.MaxValue rechnet und Progress viel Kleiner ist als MaxValue, dann würde ja 0,XX rauskommen. Div nimmt aber hier 0. Damit rechnet er mit 0*100 weiter, was natürlich 0 ist.

[Edit]
 i:= round(Gauge.Progress / Gauge.MaxValue * 100);

Grüße

Webo

_________________
Man kann nur das aus dem Ärmel schütteln, was man auch vorher reingesteckt hat.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 31.12.08 12:19 
Ganz genau, deshalb kannst du entweder das *100 vor das div schreiben oder / zur Division benutzen. Bei letzterem kannst du dann entweder mit Round auf einen ganzen Prozentbetrag runden (dann kannst du aber besser ersteres machen, denn da wäre div schneller als hinterher zu runden), oder mit Format auch eine bestimmte Anzahl an Nachkommastellen mit anzeigen.
ethillias Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41



BeitragVerfasst: Mi 31.12.08 12:21 
Ah ok danke, das wusste ich nicht.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 31.12.08 12:28 
Also hier nochmal die beiden Möglichkeiten, die ich meinte, als Quelltext:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
i := Gauge.Progress * 100 div Gauge.MaxValue;
LabelX.Caption := 'RAM: ' + IntToStr(i) + '%';
// Klammern brauchts nicht, weil * und div die gleiche Priorität haben

// oder:
LabelX.Caption := Format('RAM: %.2f %%', [Gauge.Progress / Gauge.MaxValue * 100]);
Beim ersten wird die Zahl als ganze Prozentzahl angezeigt, beim zweiten mit 2 Nachkommastellen (deshalb das .2). ;-)

Ein Runden ist so gar nicht notwendig. ;-)