Hi
I found this code on the web. I`m just learning Delphi and would
greatly appreciate some sample code showing the use of this
supplied code. The sample would have a editbox on the form
with a combobox to select the notation. When the combo selection
changes the appropriate new notation is displayed in the editbox.
Thanks
unit Notations;
interface
// modes: 0 - decimal, 1 - fractional, 2 - metric
function StringToDecimal(S: String; Mode: Integer): Extended;
function DecimalToString(D: Extended; Mode: Integer): String;
function ConvertNotations(S: String; FromMode, ToMode: Integer): String;
var
CurrentNotation: Integer;
function ToDec(S: String): Extended;
function ToStr(D: Extended): String;
implementation
uses
SysUtils;
function DecimalToFraction(D: Extended): String;
var
tn, bn, dv: Integer;
begin;
dv := 64;
tn := Trunc(D);
bn := Trunc((D - tn) * dv);
if bn > 0 then
begin
while bn mod 2 = 0 do
begin
bn := bn div 2;
dv := dv div 2;
end;
if tn = 0 then
Result := IntToStr(bn) + '/' + IntToStr(dv)
else
Result := IntToStr(tn) + ' ' + IntToStr(bn) + '/' + IntToStr(dv);
end
else
DecimalToFraction := IntToStr(tn);
end;
function FractionToDecimal(S: String): Extended;
var
pos1, pos2: Integer;
num, den: String;
begin
Result := 0;
S := Trim(S);
if Length(S) > 0 then
begin
pos1 := Pos('/', S);
if (pos1 = 0) then // it is a integer or empty
Result := StrToFloat(S)
else
begin
num := Copy(S, 1, pos1 - 1);
den := Copy(S, pos1 + 1, Length(S));
pos2 := Pos(' ', num);
if (pos2 = 0) then // no integer part
Result := StrToFloat(num) / StrToFloat(den)
else
Result := StrToInt(Copy(num, 1, pos2 - 1)) +
StrToFloat(Copy(num, pos2 + 1, Length(num))) / StrToFloat(den);
end;
end;
end;
function StringToDecimal(S: String; Mode: Integer): Extended;
begin
Result := 0;
S := Trim(S);
if Length(S) > 0 then
case Mode of
0: Result := StrToFloat(S);
1: Result := FractionToDecimal(S);
2: Result := StrToFloat(S) / 25.4;
end;
end;
function DecimalToString(D: Extended; Mode: Integer): String;
begin
Result := '';
case Mode of
0: Result := FloatToStrF(D, ffGeneral, 4, 4);
1: Result := DecimalToFraction(D);
2: Result := FloatToStrF(D * 25.4, ffGeneral, 4, 4);
end;
end;
function ConvertNotations(S: String; FromMode, ToMode: Integer): String;
begin
Result := '';
S := Trim(S);
if Length(S) > 0 then
if FromMode = ToMode then
Result := S
else
case FromMode of
0: // decimal
case ToMode of
1: // fraction
Result := DecimalToFraction(StrToFloat(S));
2: // metric
Result := FloatToStrF(StrToFloat(S) * 25.4, ffGeneral, 8, 0);
end;
1: // fraction
case ToMode of
0: // decimal
Result := FloatToStrF(FractionToDecimal(S), ffGeneral, 8, 0);
2: // metric
Result := FloatToStrF(FractionToDecimal(S) * 25.4, ffGeneral, 8, 0);
end;
2: // metric
case ToMode of
0: // decimal
Result := FloatToStrF(StrToFloat(S) / 25.4, ffGeneral, 8, 0);
1: // fraction
Result := DecimalToFraction(StrToFloat(S) / 25.4);
end;
end;
end;
function ToDec(S: String): Extended;
begin
Result := StringToDecimal(S, CurrentNotation);
end;
function ToStr(D: Extended): String;
begin
Result := DecimalToString(D, CurrentNotation);
end;
end.
I found this code on the web. I`m just learning Delphi and would
greatly appreciate some sample code showing the use of this
supplied code. The sample would have a editbox on the form
with a combobox to select the notation. When the combo selection
changes the appropriate new notation is displayed in the editbox.
Thanks
unit Notations;
interface
// modes: 0 - decimal, 1 - fractional, 2 - metric
function StringToDecimal(S: String; Mode: Integer): Extended;
function DecimalToString(D: Extended; Mode: Integer): String;
function ConvertNotations(S: String; FromMode, ToMode: Integer): String;
var
CurrentNotation: Integer;
function ToDec(S: String): Extended;
function ToStr(D: Extended): String;
implementation
uses
SysUtils;
function DecimalToFraction(D: Extended): String;
var
tn, bn, dv: Integer;
begin;
dv := 64;
tn := Trunc(D);
bn := Trunc((D - tn) * dv);
if bn > 0 then
begin
while bn mod 2 = 0 do
begin
bn := bn div 2;
dv := dv div 2;
end;
if tn = 0 then
Result := IntToStr(bn) + '/' + IntToStr(dv)
else
Result := IntToStr(tn) + ' ' + IntToStr(bn) + '/' + IntToStr(dv);
end
else
DecimalToFraction := IntToStr(tn);
end;
function FractionToDecimal(S: String): Extended;
var
pos1, pos2: Integer;
num, den: String;
begin
Result := 0;
S := Trim(S);
if Length(S) > 0 then
begin
pos1 := Pos('/', S);
if (pos1 = 0) then // it is a integer or empty
Result := StrToFloat(S)
else
begin
num := Copy(S, 1, pos1 - 1);
den := Copy(S, pos1 + 1, Length(S));
pos2 := Pos(' ', num);
if (pos2 = 0) then // no integer part
Result := StrToFloat(num) / StrToFloat(den)
else
Result := StrToInt(Copy(num, 1, pos2 - 1)) +
StrToFloat(Copy(num, pos2 + 1, Length(num))) / StrToFloat(den);
end;
end;
end;
function StringToDecimal(S: String; Mode: Integer): Extended;
begin
Result := 0;
S := Trim(S);
if Length(S) > 0 then
case Mode of
0: Result := StrToFloat(S);
1: Result := FractionToDecimal(S);
2: Result := StrToFloat(S) / 25.4;
end;
end;
function DecimalToString(D: Extended; Mode: Integer): String;
begin
Result := '';
case Mode of
0: Result := FloatToStrF(D, ffGeneral, 4, 4);
1: Result := DecimalToFraction(D);
2: Result := FloatToStrF(D * 25.4, ffGeneral, 4, 4);
end;
end;
function ConvertNotations(S: String; FromMode, ToMode: Integer): String;
begin
Result := '';
S := Trim(S);
if Length(S) > 0 then
if FromMode = ToMode then
Result := S
else
case FromMode of
0: // decimal
case ToMode of
1: // fraction
Result := DecimalToFraction(StrToFloat(S));
2: // metric
Result := FloatToStrF(StrToFloat(S) * 25.4, ffGeneral, 8, 0);
end;
1: // fraction
case ToMode of
0: // decimal
Result := FloatToStrF(FractionToDecimal(S), ffGeneral, 8, 0);
2: // metric
Result := FloatToStrF(FractionToDecimal(S) * 25.4, ffGeneral, 8, 0);
end;
2: // metric
case ToMode of
0: // decimal
Result := FloatToStrF(StrToFloat(S) / 25.4, ffGeneral, 8, 0);
1: // fraction
Result := DecimalToFraction(StrToFloat(S) / 25.4);
end;
end;
end;
function ToDec(S: String): Extended;
begin
Result := StringToDecimal(S, CurrentNotation);
end;
function ToStr(D: Extended): String;
begin
Result := DecimalToString(D, CurrentNotation);
end;
end.