2009年8月9日日曜日

Fraction

var FF11 RMT
intNumerator, intDenominator, intNegative: integer;
dblFraction, dblDecimal, dblAccuracy, dblinteger: Double;
begin
dblDecimal := decimal;
if trunc(decimal) = decimal then
result := floattostr(decimal)
else
begin
if abs(decimal) > 1 then /
begin
dblinteger := trunc(decimal);
dblDecimal := abs(frac(decimal));
end
else dblDecimal := decimal;

dblAccuracy := 0.01;
intNumerator := 0;
intDenominator := 1;
intNegative := 1;
if dblDecimal < 0 then intNegative := -1;
dblFraction := 0;
while Abs(dblFraction - dblDecimal) > dblAccuracy do
begin
if Abs(dblFraction) > Abs(dblDecimal) then
intDenominator := intDenominator + 1
else
intNumerator := intNumerator + intNegative;
dblFraction := intNumerator / intDenominator;
end;
// edit2.Text := inttostr(intNumerator) + '/' + inttostr(intDenominator);
if abs(decimal) > 1 then
result := floattostr(dblinteger) + ' ' + inttostr(intNumerator) + '/' + inttostr(intDenominator)
else
result := inttostr(intNumerator) + '/' + inttostr(intDenominator);
end;
end;

0 件のコメント:

コメントを投稿