Posts: 3,368
Threads: 195
Joined: Jan 2003
Code:
FUNCTION round# (num AS DOUBLE, dp AS INTEGER)
DIM exp1 AS LONG, num2 AS LONG
exp1 = (10 ^ dp): num2 = num * exp1: round# = (INT(num2)) / exp1
END FUNCTION
How about this for a rounding function as a replacement to the original FAQ rounding function?
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,343
Threads: 83
Joined: Mar 2003
I already made a 1 line rounder that always works (that moneo streamlined), but I don't think I added it
Posts: 1,956
Threads: 65
Joined: Jun 2003
Yes, Oracle, and here is your famous one-liner preceded by the full documentation (by yours truly) as it appears on the QBNZ tutorials.
Code:
6. ROUNDING NUMBERS (POSITIVE OR NEGATIVE):
CREDIT: Oracle of Qbasicnews and QBNZ.
The purpose is to round positive or negative numbers to the nearest whole number. The standard convention of "half-adjusting" by a rounding factor of .5 is used. Notice that positive numbers must be rounded up, and negative numbers are rounded down, that is, to a greater negative number.
NOTE: If you do INT(123.9) you will get 123. However, if you do INT(-123.9) you will get -124. This is the documented method of how the INT works in QB, which in the case of the INT(-123.9), is NOT what we want, since we want -123.
The following code takes the above conditions into consideration.
* The absolute or ABS assures that the number to work with is positive.
* This positive number is rounded by the rounding factor of "+.5".
* The integer value INT is taken on the above positive result.
* The sign of the original input number is restored by multiplying by the sign or SGN which is 1 for positive, -1 for negative, and 0 for zero.
GIVEN:
* N is the number to be rounded. Obviously, since it can have decimals, it must have a type declaration of single or double floating point.
We will define it as single (!) for this example.
* R will be the resultant rounded whole number.
R = SGN(N!) * INT(ABS(N!) + .5)
*****
Posts: 3,368
Threads: 195
Joined: Jan 2003
Doesn't do places..
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,752
Threads: 21
Joined: Jun 2002
Not hard to fix that
Code:
N! = SGN(10 ^ P * N!) * INT(ABS(10 ^ P * N!) + .51) / 10 ^ P
where P is the number of decimal places
Posts: 3,368
Threads: 195
Joined: Jan 2003
So mine is the simplest one possible then..
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,956
Threads: 65
Joined: Jun 2003
Aga, I'll have to check your solution out more carefully. However, what I usually do is scale my numbers to be rounded first, and then divide the result to re-scale to the number of decimals that I need for output. I have found that it's good practice to keep numbers as whole numbers internal to the program with implied decimals.
*****
Posts: 3,368
Threads: 195
Joined: Jan 2003
Yeah..... prevents errors.
Well a float is a float is a float.
Too bad qb4.5 doesn't have an int+decimal position or long+decimal position.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,956
Threads: 65
Joined: Jun 2003
Aga, you're right about floating point. However, you can usually avoid using floating point if you keep all your values as whole numbers internal to your program with implied decimals. In order not to go nuts as to which values have what number of implied decimals, set them all to 3 implied decimals which I have found to be sufficient for almost all cases.
*****