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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Converting lbs to Stone etc..

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, anybody have a weight convert example or someone who can help? i can convert distances like this:

procedure TfrmDistanceConverter.cmdConvertClick(Sender: TObject);
var
MileToKM, KMToMile,
KMToMetre, MetreToKM: Double;
begin
{convert based on the selected option button}

if optMileToKM.Checked then {mile > km}
begin
MileToKM:= ConvertFrom(duMiles, StrToFloat(txtInputValue.Text));
txtOutputValue.Text:= FloatToStr(MileToKM) + ' kilometers';
end

else

if optKMToMile.Checked then {km < mile}
begin
KMToMile:= ConvertTo(StrToFloat(txtInputValue.Text), duMiles);
txtOutputValue.Text:= FloatToStr(KMToMile) + ' miles';
end

else

if optKMToMetre.Checked then {km > metre}
begin
KMToMetre:= ConvertFrom(duKilometers, StrToFloat(txtInputValue.Text));
txtOutputValue.Text:= FloatToStr(KMToMetre) + ' metres';
end

else

if optMetreToKM.Checked then {metre < km}
begin
MetreToKM:= ConvertTo(StrToFloat(txtInputValue.Text), duKilometers);
txtOutputValue.Text:= FloatToStr(MetreToKM) + ' kilometers';
end
end;

but the weight doesnt work:

procedure TfrmWeightConverter.cmdConvertClick(Sender: TObject);
var
StoneToPounds, PoundsToStone: Double;
begin
{convert based on the selected option button}

if optStoneToPounds.Checked then {stone > lbs}
begin
StoneToPounds:= ConvertFrom(muStones, StrToFloat(txtInputValue.Text));
txtOutputValue.Text:= FloatToStr(StoneToPounds) + ' lbs';
end

else

if optPoundsToStone.Checked then {lbs < stone}
begin
PoundsToStone:= ConvertTo(StrToFloat(txtInputValue.Text), muPounds);
txtOutputValue.Text:= FloatToStr(PoundsToStone) + ' stone';
end;
end;

Please Help, Thanks!!!
 
I never knew these routines existed. Not that I've really had a need to use them.

When you are posting code please use the TGML tags. This makes it easier for anyone trying to help you (see below).

The ConvertFrom function converts to a base value for the measurement. The ConvertTo function converts from the base value to whatever value you specify.

So your weight converter procedure should look something like this:
Code:
procedure TfrmWeightConverter.cmdConvertClick(Sender: TObject);
var
  pounds: Double;
  stones: Double;
  baseValue: Double;
begin
  if optStonesToPounds.Checked then begin
    stones := StrToFloat( edStones.text );
    baseValue := ConvertFrom( muStones, stones );
    pounds := ConvertTo( baseValue, muPounds );
    edPounds.Text := FloatToStr( pounds );
  end
  else if optPoundsToStones.Checked then begin
    pounds := StrToFloat( edPounds.text );
    baseValue := ConvertFrom( muPounds, pounds );
    stones := ConvertTo( baseValue, muStones );
    edStones.Text := FloatToStr( stones );
  end;
end;
You might want to revisit your other conversion routines as they don't look quite right to me.

Andrew
Hampshire, UK
 
ok thanks, oh and i wasnt sure how to use the code tag thing?!

anyway the weight converts brilliant thanks, but what do you mean about my other convension routines, they seem to work ok (or am i doing something wrong with them?)

Thanks towerbase
 
ah i see what you mean, i will apply the same logic you showed me to the other routine.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top