Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use this code?

Status
Not open for further replies.

golash

Technical User
Jan 28, 2003
2
US
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.
 
You can copy the code to a form unit (interface code in interface section, implementation code in implementation section), then drop a TEdit and a TComboBox on it. Set the combo box items with the properties editor to contain the three choices:

Decimal
Fractional
Metric

Use something like this for the click event on the combo:
Code:
procedure TForm1.ComboBox1Click(Sender: TObject);
var
  NewNotation: integer;
begin
  NewNotation := ComboBox1.Items.IndexOf( ComboBox1.Text );
  Edit1.Text := ConvertNotations(Edit1.Text, CurrentNotation, NewNotation);
  CurrentNotation := NewNotation;
end;
Run.
Put a valid entry in the edit box (e.g. 1.75).
Select different combo items.

 
Hi Thanks

I was surprised I got the code working. Just started
learning Delphi two weeks ago. Am wondering what
a function would look like. Also, is that example your
choice for a application that exclusively works with
these notations. Is it possible to make a class to work
with theses notations? Any ideas are greatly appreciated.

I apologize if I`ve included too many questions
 
You can probably find better examples in the Borland demos folder.

It is rather unusual (at least in my experience) to work with fractions and convert back and forth in that way. What you found is a very specialized set of functions that may have application in the right setting, but probably not for general use.
 
Or you could create a new unit and simply cut and paste this into it as is, then save the unit as 'Notations.pas'
If you need to use one of these functions you can include 'Notations' in the 'uses' clause of you program.
Because the functions are untyped (that is they do not have the 'TForm1.' in front) you can call them from any form, this will only work if the functions do not use Form type variables etc. (which they don't appear to).

You can build up your own librarys of useful Maths and String handling funtions in this way.

If you are an Object orientated purist, you might object (sorry) to this approach I think it's ok myself.
Steve.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top