Delphi-Quellcode:
Type TIntArray =
Array of Integer;
Function RoundedPercentage(
Const Values:
Array of Integer): TIntArray;
Var i, i2: Integer;
Maximum: Int64;
ResultR:
Array of Real;
MaxR: Real;
Begin
Maximum := 0;
For i := 0
to High(Values)
do Begin
If Values[i] < 0
Then Raise Exception.Create('
Fehler');
Inc(Maximum, Values[i]);
End;
SetLength(Result, Length(Values));
SetLength(ResultR, Length(Values));
For i := 0
to High(Values)
do Begin
ResultR[i] := Values[i] / Maximum * 100;
Result[i] := Round(ResultR[i]);
End;
While True
do Begin
i2 := 0;
For i := 0
to High(Values)
do Inc(i2, Result[i]);
If i2 < 100
Then Begin
MaxR := -1;
i2 := -1;
For i := High(Values)
downto 0
do
If Round(Int(ResultR[i])) = Result[i]
Then
If Frac(ResultR[i]) > MaxR
Then Begin
MaxR := Frac(ResultR[i]);
i2 := i;
End;
If i2 < 0
Then Begin
End Else Inc(Result[i2]);
End Else If i2 > 100
Then Begin
End Else Break;
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Var R: TIntArray;
i: Integer;
Begin
R := RoundedPercentage([1545, 1545, 1545, 1545, 1545, 1545, 730]);
Caption := '
';
For i := 0
to High(R)
do
Caption := Caption + '
' + IntToStr(R[i]);
End;
hab jetzt noch nicht alle Fälle integriert, aber bei jfheins Beispiel geht es erstmal.
die fehlenden Prozente werden dann "gleichmäßig" von hinten her verteilt
Code:
15.45 15.45 15.45 15.45 15.45 15.45 7.30
15 15 15 16 16 16 7