Kieron Posted December 15, 2004 Posted December 15, 2004 Help... I need to round a number up or down to the nearest 0.5. I have tried using the round function, but it will only round to a decimal place not halves. e.g 12.3456 --- 12.5 and 173.76132 ---- 174 I have tried rounding to 2 decimal places and then depending on the last digit change to number using text function. I'm sure there must be an easier way, but I can't seem to do it. Kieron
LiveOak Posted December 15, 2004 Posted December 15, 2004 Try this: RoundedNumber (calculation, number) = Int (number) + Case ( Mod (number, 1) < .25, 0, Mod (number, 1) < .75, .5, 1) -bd
slstrother Posted December 15, 2004 Posted December 15, 2004 Here is another method, found at FM Techinfo Int(Number*2+.499999)/2 Steve
Lee Smith Posted December 15, 2004 Posted December 15, 2004 Hi Kieron, Not intending to step on LiveOaks toes or anything, but here are a couple more calculations: Int(number) + Int(( Mod(number, 1) + .49) * 2 )/2 (Int(2*number) + Sign(number)*(Mod(2*number,1)!=0))/2 HTH Lee
LiveOak Posted December 15, 2004 Posted December 15, 2004 Which calculation will be fastest??? -) -bd
-Queue- Posted December 15, 2004 Posted December 15, 2004 And Let( T = Int(4 * Mod( num; 1 )); Int(num) + .5 + .5 * (T = 3) - .5 * (not T) ) or Int(num) + Choose( Int(4 * Mod( num; 1 )) - 1; .5; .5; 1 ) Too many fun methods from which to choose.
comment Posted December 15, 2004 Posted December 15, 2004 I suppose Round (2 * InputNumber ; 0) / 2 would be too simple?
Barbecue Posted December 17, 2004 Posted December 17, 2004 OHO! An opportunity for me to do some benchmarking!
Barbecue Posted December 20, 2004 Posted December 20, 2004 I generated 20,000 random numbers, and ran each algorithm ten times against each number for a total of 200,000 iterations. I implemented each function as a set field script step. I did not generate custom functions, because my informal experiments tend to indicate that a custom function seems to have slightly more overhead than a script step. Algorithm ET O F comment 0:01:35 2 1 slstrother 0:01:36 3 1 Lee 1 0:02:00 4 3 -queue- 2 0:02:03 2 3 -queue- 1 0:02:16 7 4 Lee 2 0:02:20 6 3 LiveOak 0:03:00 3 4 Conclusions: 1. The simpler algorithms ran faster than the more complicated ones. Nothing surprising about that. 2. Function calls seem to incur more overhead than operators. The number of operators used in the algorithm appears to have less impact on the overall speed than the number of function calls did. 3. I need to get out more.
comment Posted December 20, 2004 Posted December 20, 2004 Er... I would like to thank the Members of the Academy...
comment Posted December 20, 2004 Posted December 20, 2004 On a more serious note: speed may be important, but correct results should come first. This calculation (1st runner-up): Int(Number*2+.499999)/2 incorrectly returns 0 (instead of 0.5) when Number=0.25; This calculation (2nd runner-up): Int(number) + Int(( Mod(number, 1) + .49) * 2 )/2 rounds UP, i.e. when Number=0.1, the result returned is 0.5. However, when Number=0.09 or less, the result will be 0.
Recommended Posts
This topic is 7277 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now