eNarc
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??
2.1 * 3.4 = 7.14
how do I do that in delphi??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
procedure TForm1.Button1Click(Sender : TObject);
var
myvalue :extended;
begin
myvalue := 2.1 * 3.4;
myvalue := round(myvalue*100)/100;
showmessage(floattostr(myv alue));
end;
var
myvalue :extended;
begin
myvalue := 2.1 * 3.4;
myvalue := round(myvalue*100)/100;
showmessage(floattostr(myv
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(rou nd((2.1 * 3.4)*100)/100));
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(rou
Er didn't answer your question there: For 4 decimal places, try:
showmessage(floattostr(rou nd((2.1 * 3.4)*10000)/10000));
showmessage(floattostr(rou
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(Rou ndTo( c, -4)));
var a, b, c: single;
begin
a := 2.1;
b := 3.4;
c := a * b;
showmessage(floattostr(Rou
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
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;
procedure TForm1.Button1Click(Sender
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!
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".
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
Computer101
EE Admin
var a, b, c: single:
begin
a := 2.1;
b := 3.4;
c := a * b;
end;