Sonny,
I am multiplying by 100 before Round2, as suggested by mc73, because if i use only round2 for rounding and then do the checking of the equality of float or double variables i don't have the needed accuracy. Again with some numbers returning "not equal"
I changed the number for my second example and removed the "correction factor" trying to examine if setting variable types to double gave me accuracy in equality checking with every possible number.
What method do you suggest?
Pantelis,
When you say "accuracy", what is it are you really after? Accuracy as in 2 decimal places, ie; 100.23 or 92.71? Please explain.
Here's an example of what we used to do back in the "wonderful days of Fortran programming" a long time ago (back when I still had hair). In this example, we're looking for 2 decimal places of rounding and "accuracy":
- assume that your result is; 100.234xxxxxxxxx
- and you want a rounded accuracy of; 100.xx
- so add "0.005" to the result -> you get; 100.239xxxxxxxxx
- multiply the result by 100 -> you get; 10023.9xxxxxxxxx
- take the INT of the number -> you get; 10023
- divide the number by 100 -> you get; 100.23
When you add 0.005 to the number, this causes 0.234 to advance to 0.239 which will give you the correct rounding when you perform the other steps.
If the number were 100.235 instead, then the result would be; 100.240; so when you perform the remaining steps you'll get; 100.24; which is what you're after, right?
This is what the Round2 function does - behind the scenes - so if you multiply before you Round2, you'll lose the desired effect. Round2 first, then multiply.
Round2(Number as double, Decimal Places as int) as double
"result = Round(100.234xxxxxxxxx, 2)" should return the same result.
You were doing; "Round2((a * 100),0)". The zero means "don't perform any decimal rounding at all - just truncate the entire decimal portion and return the integer portion."
Read the documentation - it states; "Rounds the given number and leaves up to the specified number of fractional digits", meaning, if you want to round to a precision of; "abc.xy", then replace the zero with "2".
I hope I'm understanding your question correctly, and am giving you useful information.
> Sonny <