| View previous topic :: View next topic |
| Author |
Message |
Phlox Guest
|
Posted: Wed Jul 30, 2003 11:16 am Post subject: RoundTo Problem with Delphi |
|
|
Hi,
is Delphi able to round in the following way:
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
Thanks
|
|
| Back to top |
|
 |
John Leavey Guest
|
Posted: Wed Jul 30, 2003 12:43 pm Post subject: Re: RoundTo Problem with Delphi |
|
|
On Wed, 30 Jul 2003 13:16:31 +0200, "Phlox" <Master_Phlox (AT) web (DOT) de> wrote:
| Quote: | Hi,
is Delphi able to round in the following way:
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
|
RoundTo() calls Round() which uses "banker's rounding" - rounds towards nearest even
number.
For the rounding you want, replace RoundTo() with this :-
function MyRoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
var
LFactor: Double;
begin
LFactor := IntPower(10, ADigit);
Result := Trunc( ( AValue / LFactor ) + 0.5) * LFactor;
end;
John Leavey
|
|
| Back to top |
|
 |
Yahia El-Qasem Guest
|
Posted: Wed Jul 30, 2003 4:00 pm Post subject: Re: RoundTo Problem with Delphi |
|
|
Use SimpleRoundTo from the Delphi RTL - it uses the rounding algorithm you
want...
Yahia
|
|
| Back to top |
|
 |
Achim Berg Guest
|
Posted: Wed Jul 30, 2003 8:22 pm Post subject: Re: RoundTo Problem with Delphi |
|
|
Use SimpleRoundTo
Achim
"Phlox" <Master_Phlox (AT) web (DOT) de> schrieb im Newsbeitrag
news:3f27a970$2 (AT) newsgroups (DOT) borland.com...
| Quote: | Hi,
is Delphi able to round in the following way:
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
Thanks
|
|
|
| Back to top |
|
 |
Martin Stoeckli Guest
|
Posted: Thu Jul 31, 2003 7:37 am Post subject: Re: RoundTo Problem with Delphi |
|
|
Hello
SimpleRoundTo would be the function to use, but unfortunately it has
problems with negative numbers.
example:
-8.4 will be rounded to -7
The problem is already logged.
Best regards:
Martin
"Achim Berg" <Achim.Berg (AT) Corporate-Planning (DOT) com> schrieb im Newsbeitrag
news:3f2828f5$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Use SimpleRoundTo
Achim
"Phlox" <Master_Phlox (AT) web (DOT) de> schrieb im Newsbeitrag
news:3f27a970$2 (AT) newsgroups (DOT) borland.com...
Hi,
is Delphi able to round in the following way:
1.235 -> 1.24
1.245 -> 1.25
The RoundTo function is retuning eiter way 1.24 !!!!
Thanks
|
|
|
| Back to top |
|
 |
|