Board index » delphi » Round(Value, NumberOfDecimals)

Round(Value, NumberOfDecimals)


2006-07-14 05:50:07 PM
delphi185
Hi all
Okay, the question is technical, but where it belongs I just don't know!
I have an Extended variable with a value something like 1.999343434, at the
moment I round it and get 2. Sometimes the value will be more like
1.4999999 in which case I'd like to see 1.5 instead of 1.
Is there a Round function which takes a second parameter indicating how many
decimal places to round to?
PS, don't you think there are just too many different Delphi groups on this
server? I often find that the most appropriate looking group has little or
no posts.
--
Pete
====
Audio compression components, DIB graphics controls, ECO extensions,
FastStrings
www.droopyeyes.com
My blog
blogs.slcdug.org/petermorris/
 
 

Re:Round(Value, NumberOfDecimals)

I think this can help you:
ROUNDTO:
Rounds a floating-point value to a specified digit or power of ten using
"Banker's rounding".
Unit
Math
Delphi syntax:
type TRoundToRange = -37..37;
function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
C++ syntax:
typedef Word TRoundToRange;
extern PACKAGE double __fastcall RoundTo(const double AValue, const
TRoundToRange ADigit);
Description
Call RoundTo to round AValue to a specified power of ten.
AValue is the value to round.
ADigit indicates the power of ten to which you want AValue rounded. It can
be any value from -37 to 37 (inclusive).
RoundTo uses "Banker's Rounding" to determine how to round values that are
exactly midway between the two values that have the desired number of
significant digits. This method rounds to an even number in the case that
AValue is not nearer to either value.
 

Re:Round(Value, NumberOfDecimals)

Thanks very much, that is exactly what I was looking for. I expected it to
be linked from the "See also" section of "Round".
 

Re:Round(Value, NumberOfDecimals)

"Peter Morris
Quote
I have an Extended variable with a value something
like 1.999343434, at the moment I round it and get 2.
Sometimes the value will be more like 1.4999999
in which case I'd like to see 1.5 instead of 1.
Is there a Round function which takes a second
parameter indicating how many decimal places to
round to?
Peter,
Michele's suggestion would be OK except, that
RoundTo and SimpleRoundTo are sick. See
qc.borland.com/wc/qcmain.aspx
qc.borland.com/wc/qcmain.aspx
To accomplish what you want, I wrote my DecimalRounding
routines, which you can download from CodeCentral at
codecentral.borland.com/Item.aspx
These routines can do about 9 kinds of rounding and
do account for the inability of floating binary point
numbers to exactly represent decimal fractions.
Quote
Okay, the question is technical,
but where it belongs I just don't know!
Perhaps,
borland.public.delphi.language.delphi.general
Quote
PS, don't you think there are just too many different Delphi
groups on this server? I often find that the most appropriate
looking group has little or no posts.
Perhaps, but I often do a Google Groups search to get a clue.
groups.google.com/groups/search
Best regards, JohnH
 

Re:Round(Value, NumberOfDecimals)

Peter Morris [Droopy eyes software] writes:
Quote
I have an Extended variable with a value something like 1.999343434, at the
moment I round it and get 2. Sometimes the value will be more like
1.4999999 in which case I'd like to see 1.5 instead of 1.

Is there a Round function which takes a second parameter indicating how many
decimal places to round to?
If you want to round to 2 decimal places:
round(1.4999999 * 100) / 100
= round(149.99999) / 100
= 150 / 100
= 1.50
3 decimal places:
round(1.4999999 * 1000) / 1000
etc..
Regards,
M. Said
 

Re:Round(Value, NumberOfDecimals)

M. Said a écrit :
Quote
If you want to round to 2 decimal places:
round(1.4999999 * 100) / 100
It depends on the adjustment, for a mathematical rounding:
floor(1.49999999 * 100 + 0.5) / 100
regards,
--
Florent Ouchet
 

Re:Round(Value, NumberOfDecimals)

John,
I downloaded your unit and I thank you for your great work!
I do have a question. In QC8070 you say "In the later DecimalRounding_JH1
version I documented that and added an Assert(MaxRelError>0, ...)
statement." but in JH1 I cannot find that statement. I downloaded
DecimalRounding_JH1.pas from code central and the date is 6/29/2004. Do I
have an earlier version than what you have?
Thanks!
 

Re:Round(Value, NumberOfDecimals)

On Fri, 14 Jul 2006 11:21:22 +0100, Peter Morris [Droopy eyes
software] writes:
Quote
Thanks very much, that is exactly what I was looking for. I expected it to
be linked from the "See also" section of "Round".
A good idea. Why don't you put it in QC.
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
 

Re:Round(Value, NumberOfDecimals)

"Jack MacRank" <XXXX@XXXXX.COM>wrote
Quote
... In QC8070 you say "In the later DecimalRounding_JH1
version I documented that and added an
Assert(MaxRelError>0, ...)
statement." but in JH1 I cannot find that statement. I down-
loaded DecimalRounding_JH1.pas from code central and
the date is 6/29/2004. Do I have an earlier version than
what you have?
Jack,
Thanks for the reminder. I added the Assert about a year ago,
but could not log into CodeCentral to update the file. But, I
could today, and just update the file and added an entry to the
summary to so indicate. See
cc.borland.com/item.aspx
The assert was only to prevent a misuse of the code. Read
the header of the code for more information about it.
Regards, JohnH
 

Re:Round(Value, NumberOfDecimals)

Quote
A good idea. Why don't you put it in QC.
I can not answer why I wouldn't, especially on account that I did :-)