Link to home
Start Free TrialLog in
Avatar of eNarc
eNarcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Maths 2.1 * 3.4 = 7.14

Hi having a problem using a decimal point

2.1 * 3.4 = 7.14


how do I do that in delphi??

Avatar of MerijnB
MerijnB
Flag of Netherlands image

euhm, what is wrong with this?

var a, b, c: single:
begin
 a := 2.1;
 b := 3.4;
 c := a * b;
end;
ASKER CERTIFIED SOLUTION
Avatar of rfwoolf
rfwoolf
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eNarc

ASKER

that works :) answer is 7.1399998664856

how could I make that digit like 7.1399 so its 4 digits after the decimal point? would be great if I could select how many like 0 or 2 or 5 or something, how could I do that?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
procedure TForm1.Button1Click(Sender: TObject);
var
myvalue :extended;
begin
myvalue := 2.1 * 3.4;
myvalue := round(myvalue*100)/100;
showmessage(floattostr(myvalue));
end;
To explain the above:
an equation such as 2.1 * 3.4 as you know results in a number with a lot of digits, i.e. 7.1399998664856
This cannot be stored using the normal integer data type, instead you can use something such as single or extended (see the delphi help file for more info).
That's why to convert the value to a string I used FloatToStr instead of IntToStr.

A short example of the answer would be:
showmessage(floattostr(round((2.1 * 3.4)*100)/100));
Er didn't answer your question there: For 4 decimal places, try:
showmessage(floattostr(round((2.1 * 3.4)*10000)/10000));
you might want to be careful with that approach.
Singles, doubles, reals, extendeds, etc are only approximations of values.
This means that it is possible that (round((7.1399998664856 * 10000)/1000) <> 7.1399
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use RoundTo in math unit. See it's help.

  var a, b, c: single;
begin
 a := 2.1;
 b := 3.4;
 c := a * b;
 showmessage(floattostr(RoundTo( c, -4)));
RoundTo() also uses bankers rounding
i didn't get you
afaik, all standard rounding routines in Delphi use bankers rounding, you don't always want that:

http://www.xbeat.net/vbspeed/i_BankersRounding.htm
In your case it might be simpler to just use the currency data type.

procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, C : currency;

begin
  A := 2.1;
  B := 3.4;
  C := A * B;
  Caption := FloatToStr(C);
end;
ciuly, you have the weekend of or something? You are generating a _lot_ of spam in this mailbox ;) Keep up the good work!
Yes, ciuly, what were you thinking?
The community thanks you!
https://www.experts-exchange.com/springCleanupScoreboard.jsp?type=20

the first 10 CVs get a coffe mug. I'm after that coffe mug :lol:
the contest is over though so I hope I made it to top10. it'll be another few days until they count all the "points".
Forced accept.

Computer101
EE Admin