Hi Group
I found this code and but don`t have a clue how to incorporate it into a project.
I wanted to have a set of option buttons that the user can select to pick the
format of all data displayed in the form controls. i.e.. 1/8 or .125 or 7mm.
This novice needs your expert help on this one. Thanks
___________________________________________________________
Is there any way to streamline this implementation section of the code. I have
to write quite a few formula functions based off of this fractional code. As it is it looks
now it will be very inconvient to deal with this long winded syntax
procedure TForm1.Button2Click(Sender: TObject);
begin
edDSDest.Text := DS(StrToFloat(edDSSource.Text),radDSConvMode.ItemIndex);
end;
___________________________________________________________
end.
unit Numerics;
interface
const
cmSTRING_TO_NUMERIC = 0;
cmFRACTION_TO_DECIMAL = 1;
cmMETRIC_TO_IMPERIAL = 2;
nfDECIMAL = 0;
nfFRACTION = 1;
nfMETRIC = 2;
MM_PER_INCH = 25.4;
function Convert(S : String; OrgMode : Integer; ToMode : Integer) : String;
function CV( S : String; Mode : Integer) : Double;
function ds(d : Double; Mode : Integer) : String;
function DecimalToFraction(wn : Double) : string;
function fraction2dec(s : string) : Double;
implementation
uses
SysUtils;
function fraction2dec(s : string) : Double;
var
PosDiv : integer;
PosSpace : Integer;
Num : string;
Den : string;
Int : string;
Fract : string;
begin
try
S := trim(s);
if Length(s) = 0 then begin
Result := 0;
Exit;
end;
PosDiv := pos('/',S);
if PosDiv = 0 then
// String holds an integer.
Result := StrToFloat(s)
else begin
Num := trim(copy(s,1,PosDiv-1));
Den := trim(copy(s,PosDiv+1,length(s)-PosDiv));
PosSpace := pos(' ',Num);
if PosSpace = 0 then
Result := StrToInt(Num)/StrToInt(Den)
else begin
Int := trim(copy(Num,1,PosSpace-1));
Fract := trim(copy(Num,PosSpace+1,Length(Num)-PosSpace));
Result := StrToInt(Int) + StrToint(Fract)/StrtoInt(Den);
end;
end;
except
on EConvertError do
Result := 0;
end;
end;
function CV( S : String; Mode : Integer) : Double;
begin
S := Trim(S);
If Length(s) > 0 Then
case Mode of
cmSTRING_TO_NUMERIC : Result := StrToFloat(s);
cmFRACTION_TO_DECIMAL : Result := fraction2dec(s);
cmMETRIC_TO_IMPERIAL : Result := StrToFloat(s)/MM_PER_INCH;
else Result := 0;
end // Case
else // DMc 22/01/03
Result := 0;
end;
function DecimalToFraction(wn : Double) : string;
var
tn : Integer;
bn : Integer;
dv : Integer;
begin
dv := 64;
tn := Trunc(wn);
bn := trunc(0.5+(wn - tn) * dv); // DMc 22/01/03
if bn > 0 then begin
while trunc(bn/2) = (bn/2) do begin
bn := trunc(bn/2);
dv := trunc(dv/2);
end;
if tn = 0 then
Result := Format('%d/%d',[bn,dv])
else
result := Format('%d %d/%d',[tn,bn,dv]);
end
else
Result := IntToStr(tn);
end;
function Convert(S : String; OrgMode : Integer; ToMode : Integer) : String;
begin
s := trim(s);
if Length(s) = 0 then begin
Result := ''
end
else
if orgmode = tomode then
Result := s
else begin
if (OrgMode = nfDECIMAL) and (ToMode = nfFRACTION) then
Result := DecimalToFraction(StrToFloat(S))
else if (OrgMode = nfDECIMAL) and (ToMode = nfMETRIC) then
Result := FloatToStr(StrToFloat(S)*MM_PER_INCH)
else if (OrgMode = nfFRACTION) and (Tomode = nfDECIMAL) then
Result := FloatToStr(fraction2dec(s))
else if (OrgMode = nfFraction) and (ToMode = nfMETRIC) then
Result :=FloatToStr(fraction2dec(s)*MM_PER_INCH)
else if (orgmode = nfMETRIC) and (tomode = nfDECIMAL) then
Result := FloatToStr(StrToFloat(s)/MM_PER_INCH)
else if (OrgMode = nfMetric) and (ToMode = nfFraction) then
Result := DecimalToFraction(StrToFloat(s) / MM_PER_INCH)
end;
end;
function ds(d : Double; Mode : Integer) : String;
begin
case Mode of
0 : Result := FloatToStr(d);
1 : Result := DecimalToFraction(d);
2 : Result := FloatToStr(d*MM_PER_INCH);
end;
end;
end.
I found this code and but don`t have a clue how to incorporate it into a project.
I wanted to have a set of option buttons that the user can select to pick the
format of all data displayed in the form controls. i.e.. 1/8 or .125 or 7mm.
This novice needs your expert help on this one. Thanks
___________________________________________________________
Is there any way to streamline this implementation section of the code. I have
to write quite a few formula functions based off of this fractional code. As it is it looks
now it will be very inconvient to deal with this long winded syntax
procedure TForm1.Button2Click(Sender: TObject);
begin
edDSDest.Text := DS(StrToFloat(edDSSource.Text),radDSConvMode.ItemIndex);
end;
___________________________________________________________
end.
unit Numerics;
interface
const
cmSTRING_TO_NUMERIC = 0;
cmFRACTION_TO_DECIMAL = 1;
cmMETRIC_TO_IMPERIAL = 2;
nfDECIMAL = 0;
nfFRACTION = 1;
nfMETRIC = 2;
MM_PER_INCH = 25.4;
function Convert(S : String; OrgMode : Integer; ToMode : Integer) : String;
function CV( S : String; Mode : Integer) : Double;
function ds(d : Double; Mode : Integer) : String;
function DecimalToFraction(wn : Double) : string;
function fraction2dec(s : string) : Double;
implementation
uses
SysUtils;
function fraction2dec(s : string) : Double;
var
PosDiv : integer;
PosSpace : Integer;
Num : string;
Den : string;
Int : string;
Fract : string;
begin
try
S := trim(s);
if Length(s) = 0 then begin
Result := 0;
Exit;
end;
PosDiv := pos('/',S);
if PosDiv = 0 then
// String holds an integer.
Result := StrToFloat(s)
else begin
Num := trim(copy(s,1,PosDiv-1));
Den := trim(copy(s,PosDiv+1,length(s)-PosDiv));
PosSpace := pos(' ',Num);
if PosSpace = 0 then
Result := StrToInt(Num)/StrToInt(Den)
else begin
Int := trim(copy(Num,1,PosSpace-1));
Fract := trim(copy(Num,PosSpace+1,Length(Num)-PosSpace));
Result := StrToInt(Int) + StrToint(Fract)/StrtoInt(Den);
end;
end;
except
on EConvertError do
Result := 0;
end;
end;
function CV( S : String; Mode : Integer) : Double;
begin
S := Trim(S);
If Length(s) > 0 Then
case Mode of
cmSTRING_TO_NUMERIC : Result := StrToFloat(s);
cmFRACTION_TO_DECIMAL : Result := fraction2dec(s);
cmMETRIC_TO_IMPERIAL : Result := StrToFloat(s)/MM_PER_INCH;
else Result := 0;
end // Case
else // DMc 22/01/03
Result := 0;
end;
function DecimalToFraction(wn : Double) : string;
var
tn : Integer;
bn : Integer;
dv : Integer;
begin
dv := 64;
tn := Trunc(wn);
bn := trunc(0.5+(wn - tn) * dv); // DMc 22/01/03
if bn > 0 then begin
while trunc(bn/2) = (bn/2) do begin
bn := trunc(bn/2);
dv := trunc(dv/2);
end;
if tn = 0 then
Result := Format('%d/%d',[bn,dv])
else
result := Format('%d %d/%d',[tn,bn,dv]);
end
else
Result := IntToStr(tn);
end;
function Convert(S : String; OrgMode : Integer; ToMode : Integer) : String;
begin
s := trim(s);
if Length(s) = 0 then begin
Result := ''
end
else
if orgmode = tomode then
Result := s
else begin
if (OrgMode = nfDECIMAL) and (ToMode = nfFRACTION) then
Result := DecimalToFraction(StrToFloat(S))
else if (OrgMode = nfDECIMAL) and (ToMode = nfMETRIC) then
Result := FloatToStr(StrToFloat(S)*MM_PER_INCH)
else if (OrgMode = nfFRACTION) and (Tomode = nfDECIMAL) then
Result := FloatToStr(fraction2dec(s))
else if (OrgMode = nfFraction) and (ToMode = nfMETRIC) then
Result :=FloatToStr(fraction2dec(s)*MM_PER_INCH)
else if (orgmode = nfMETRIC) and (tomode = nfDECIMAL) then
Result := FloatToStr(StrToFloat(s)/MM_PER_INCH)
else if (OrgMode = nfMetric) and (ToMode = nfFraction) then
Result := DecimalToFraction(StrToFloat(s) / MM_PER_INCH)
end;
end;
function ds(d : Double; Mode : Integer) : String;
begin
case Mode of
0 : Result := FloatToStr(d);
1 : Result := DecimalToFraction(d);
2 : Result := FloatToStr(d*MM_PER_INCH);
end;
end;
end.