Grade 10 · Data & calculations · Lesson and coding task · about 14 minutes
Open this task in the Delphi workspace →
Users type into edit boxes (TEdit). Whatever they type is text, so edtPrice.Text is a String — even if they typed 100. To calculate with it you must convert:
rPrice := StrToFloat(edtPrice.Text); // text -> Real
iAge := StrToInt(edtAge.Text); // text -> Integer
And to display an answer you convert back. For money we want exactly two decimals, so we use FloatToStrF:
lblTotal.Caption := 'Total: R ' + FloatToStrF(rTotal, ffFixed, 10, 2);
ffFixed, 10, 2 means: fixed notation, up to 10 significant digits, 2 decimals. So 115 becomes 115.00 — proper till-slip money.
Your task: a VAT calculator
South Africa charges 15% VAT. Given a price excluding VAT:
rVAT := rPrice * 0.15;
rTotal := rPrice + rVAT; // or rPrice * 1.15
FloatToStrF(…, ffCurrency, …) shows the Rand symbol automatically, using your Regional Settings (often with a decimal comma: R 115,00). In exams you will usually build the R yourself as we do here.What you will do
- Read the price from edtPrice with StrToFloat
- Calculate the VAT (15%) and the total including VAT
- Show the total on lblTotal as: Total: R 115.00 (2 decimals)
- Add a memo line: VAT: R 15.00
Hints
- rPrice := StrToFloat(edtPrice.Text);
- VAT is rPrice * 0.15. The total is the price plus the VAT.
- FloatToStrF(rTotal, ffFixed, 10, 2) gives '115.00' — put 'Total: R ' in front of it.
Model solution
procedure TfrmVAT.btnCalculateClick(Sender: TObject);
var
rPrice, rVAT, rTotal : Real;
begin
rPrice := StrToFloat(edtPrice.Text);
rVAT := rPrice * 0.15;
rTotal := rPrice + rVAT;
lblTotal.Caption := 'Total: R ' + FloatToStrF(rTotal, ffFixed, 10, 2);
memOutput.Lines.Add('VAT: R ' + FloatToStrF(rVAT, ffFixed, 10, 2));
end;