Re: fractions summing to a string instead of a number

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



However, the sum could just as easily be 99.9999999993 instead of
100.00000000003, in which case Int or Fix won't work.

The usual approach from Numerical Methods is to look at the difference
between the number and the desired number, and see whether they're "close
enough".

Instead of

If [total] <> 1 Then

use

If Abs([total] - 1) > 0.0001 Then

(You may have to play with what value to use for "close enough")

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"Klatuu" <Klatuu@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D208869E-E845-43F6-B0F9-892B74C1838E@xxxxxxxxxxxxxxxx
This has nothing to do with whether it is a string or not. It has to do
with
rounding.
When your percentages are calculated, it is almost a certainty some of the
calculations will result in some decimal that is masked by your
formatting.
So that the whole may appear to by 100%, but in fact it is something like
100.0000000003 (for example). That, then, will not evaluate to true. The
easy fix is to compare on the Int or the Fix of the total. That will drop
the decimals and evaluate to 100%
--
Dave Hargis, Microsoft Access MVP


"Adam Bryce" wrote:

Hi,

I came across this when coding a form in Acc2002, and found I could
replicate it, but had no idea why it happens:

I create a form with four text boxes: A, B, C and Total
A - C have a default value of zero, and an afterUpdate event that calls a
function within the form code:

If [total] <> 1 Then
[total].forecolor = 255 'highlight text (red)
Else
[total].forecolor = 0 'normal text (black)
End If

The Total Box has a control source of:

=cdbl([A])+cdbl([B])+cdbl([C])

all the boxes are formatted as percentage*, with no d.p. - the idea being
that if they don't sum to 100%, the user is warned by the total turning
red.
*I've tried it without formatting, and it doesn't change the problem at
all.

My problem is that, with certain combinations of A, B and C, the box
turns
red even if the values sum to 100.

For example having A-C being 30%, 35%, 35% respectively results in '100%'
displayed in red. However, A-C entered as 35%, 35% and 30% results in
'100%'
displayed in black.

Having checked the code, it turns out that in the former case, the value
of
[total] is being returned as "1" instead of 1.

I've worked around this in my real code by just changing the condition to
<>
"1", but I'm still perplexed as to why the order of the numbers in the
expression changes the type of the result.

Can anyone help explain this, or even better, tell me where I'm going
wrong
and how to avoid it in future?

Thanks,
Adam.



.



Relevant Pages

  • What is wrong with my C program?
    ... the program computes the sum of all the ... Could you, please, help me to fix this program? ... int main ...
    (alt.comp.lang.learn.c-cpp)
  • [git patches] net driver updates for .26
    ... Fix a bug where the pointer never moves for dma_unmap... ... Update and fix driver debugging messages ... int reset); ... * header structure can be anywhere in the mcp. ...
    (Linux-Kernel)
  • [git patches] net driver fixes
    ... Fix NAPI state bug when Rx complete ... const struct cmac_statistics *s; ... int flag) ...
    (Linux-Kernel)
  • [git patches] net driver updates
    ... Fix locking in ethtool support ... static int au1000_init ... if (err) { ... * @adapter: board private struct ...
    (Linux-Kernel)
  • [git patches] net driver updates
    ... Fix PHY Lib support for gianfar and ucc_geth ... int mii_status; ... struct vio_dev *vio_dev) ... struct net_device *dev; ...
    (Linux-Kernel)