Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 20 total
Thread Problems with rounding figures (floats)
Wed, Jun 27 2007 1:44 AMPermanent Link

"Adam H."
Hi,

For a while I've been having problems when storing numeric values in
databases with the values changing by some nth of a decimal place. This
first originated years ago when I was using the BDE. (Apparently it has
something to do with the way float values are stored).

With an application I've designed recently, I decided to get around this by
using BCDFields, but it doesn't seem to have fixed my problem, and I was
wondering if someone knew some 'tricks'.

The BCDFields that I have are set to a 4 decimal place. However - they will
*almost* always only ever be rounded to 2 decimal places. I use script such
as:

myvalue := trunc(myvalue * 100)*0.01  to round any values to 2 decimalplaces
before storing them in a table.

However, I'm finding that clients are reporting up to 4 decimal places
stored in their database applications. (ie, if a value is 25.02., they may
have 25.0201 stored instead). The value that it changes by seems to be
always 0.0001.

I'm using Delphi 7 on Win XP machines. This doesn't always occur, but it
occurs enough to be a problem. Is their some setting I can change to fix
this floating issue?

I can't change my BCDFields to 2 decimal places, as their may be an instant
where they do need to go to three, or even four decimal places, but it's
extremely rare.

Thanks & Regards

Adam,.

Wed, Jun 27 2007 7:48 AMPermanent Link

"Robert"

"Adam H." <ahairsub4@rREMOVEMEspamSTOPPER.jvxp.com> wrote in message
news:4030A007-F4AE-44BF-B527-6C2E0091E4C7@news.elevatesoft.com...
>
> myvalue := trunc(myvalue * 100)*0.01  to round any values to 2
> decimalplaces before storing them in a table.
>

Try this - it works for me

uses math;

Function RoundScale(const x: Currency; const scale: Integer): Currency;
var intero : Currency;
begin
 If x>0 then intero := int(x*Power(10, scale) + 0.5)
 else if x<0 then intero := int(x*Power(10,scale) - 0.5)
 else intero := 0;
 Result := intero / Power(10, scale);
end;

There have been volumes written on this subject.

Robert


Wed, Jun 27 2007 10:24 AMPermanent Link

Dave Harrison
Robert wrote:

> "Adam H." <ahairsub4@rREMOVEMEspamSTOPPER.jvxp.com> wrote in message
> news:4030A007-F4AE-44BF-B527-6C2E0091E4C7@news.elevatesoft.com...
>
>>myvalue := trunc(myvalue * 100)*0.01  to round any values to 2
>>decimalplaces before storing them in a table.
>>
>
>
> Try this - it works for me
>
> uses math;
>
> Function RoundScale(const x: Currency; const scale: Integer): Currency;
> var intero : Currency;
> begin
>   If x>0 then intero := int(x*Power(10, scale) + 0.5)
>   else if x<0 then intero := int(x*Power(10,scale) - 0.5)
>   else intero := 0;
>   Result := intero / Power(10, scale);
> end;
>
> There have been volumes written on this subject.
>
> Robert
>
>

Is there a reason why you guys don't want to use the RoundTo() function?
As in RoundTo(1.235,-2) becomes 1.24.

Dave
Wed, Jun 27 2007 1:44 PMPermanent Link

Sanford Aranoff


>
> >
>
> Is there a reason why you guys don't want to use the RoundTo() function?
> As in RoundTo(1.235,-2) becomes 1.24.
>
> Dave

What about banker's rounding?
Wed, Jun 27 2007 6:33 PMPermanent Link

"Adam H."
Hi Robert,

> Try this - it works for me
>
> uses math;
>
> Function RoundScale(const x: Currency; const scale: Integer): Currency;
> var intero : Currency;
> begin
>  If x>0 then intero := int(x*Power(10, scale) + 0.5)
>  else if x<0 then intero := int(x*Power(10,scale) - 0.5)
>  else intero := 0;
>  Result := intero / Power(10, scale);
> end;
>
> There have been volumes written on this subject.

Thanks. I'll give that a go, however I don't understand why this should
work, and my previous example leave decimal values right at the end. I've
done some searching on the topic, but haven't been able to find something
helpful todate (maybe I'm using the wrong search words), which is why I
posted here.

Cheers

Adam.

Thu, Jun 28 2007 7:05 AMPermanent Link

"Walter Matte"
http://cc.codegear.com/Author/358

John Herbster submitted to Code Central a unit "DecimalRounding (JH1)".

To overcome IEEE-754 Floats.

Has options for Bankers Rounding.. etc..

Walter

"Adam H." <ahairsub4@rREMOVEMEspamSTOPPER.jvxp.com> wrote in message
news:4030A007-F4AE-44BF-B527-6C2E0091E4C7@news.elevatesoft.com...
> Hi,
>
> For a while I've been having problems when storing numeric values in
> databases with the values changing by some nth of a decimal place. This
> first originated years ago when I was using the BDE. (Apparently it has
> something to do with the way float values are stored).
>
> With an application I've designed recently, I decided to get around this
> by using BCDFields, but it doesn't seem to have fixed my problem, and I
> was wondering if someone knew some 'tricks'.
>
> The BCDFields that I have are set to a 4 decimal place. However - they
> will *almost* always only ever be rounded to 2 decimal places. I use
> script such as:
>
> myvalue := trunc(myvalue * 100)*0.01  to round any values to 2
> decimalplaces before storing them in a table.
>
> However, I'm finding that clients are reporting up to 4 decimal places
> stored in their database applications. (ie, if a value is 25.02., they may
> have 25.0201 stored instead). The value that it changes by seems to be
> always 0.0001.
>
> I'm using Delphi 7 on Win XP machines. This doesn't always occur, but it
> occurs enough to be a problem. Is their some setting I can change to fix
> this floating issue?
>
> I can't change my BCDFields to 2 decimal places, as their may be an
> instant where they do need to go to three, or even four decimal places,
> but it's extremely rare.
>
> Thanks & Regards
>
> Adam,.
>

Thu, Jun 28 2007 7:28 AMPermanent Link

"Robert"

"Dave Harrison" <daveh_18824@spammore.com> wrote in message
news:28FB7FEF-A52A-4D40-AEFA-7A47DD13390B@news.elevatesoft.com...
>
> Is there a reason why you guys don't want to use the RoundTo() function?
> As in RoundTo(1.235,-2) becomes 1.24.
>

There is something about using the currency type to do the rounding. I don't
remember the details. If you do the same thing using float, it does not work
the same way.

Robert

Thu, Jun 28 2007 7:42 AMPermanent Link

"Robert"

"Walter Matte" <walter_@_interlog.com> wrote in message
news:31CCB4D0-1E7D-456C-8620-5AE16BDD057B@news.elevatesoft.com...
> http://cc.codegear.com/Author/358
>
> John Herbster submitted to Code Central a unit "DecimalRounding (JH1)".
>
> To overcome IEEE-754 Floats.
>

Yeah, a bit like using a shotgun to kill a mosquito, but it works.

Robert

Thu, Jun 28 2007 8:06 AMPermanent Link

"Adam H."
Hi Walter,

> http://cc.codegear.com/Author/358

Thanks for the link, but I'm getting an "Server application Unavailable"
error. Is anyone else able to access this?

Thanks

Adam.

Thu, Jun 28 2007 8:08 AMPermanent Link

"Adam H."
Hi Robert,

>> Is there a reason why you guys don't want to use the RoundTo() function?
>> As in RoundTo(1.235,-2) becomes 1.24.
>>
>
> There is something about using the currency type to do the rounding. I
> don't remember the details. If you do the same thing using float, it does
> not work the same way.

I have read that to get around this problem one should use currency or BCD's
instead of  doubles for fields, however this problem still happens even
though I use BCD's. (Well, happens to 4 decimal places anyway).

Cheers

Adam.

Page 1 of 2Next Page »
Jump to Page:  1 2
Image