Round to the nearest .5 value
I'm trying to create a ratings system that shows 1/2 values. Quick question, what would be the easiest way to round a value to the nearest .5 of a number? I'm trying to avoid multiple if/elses. i.e. 3.21 would round to 3 i.e. 3.31 would round to 3.5 i.e. 3.61 would round to 3.5 i.e. 3.91 would round to 4 Thanks, Che ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328685 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Re: Round to the nearest .5 value
Assuming 'n' is your number, use #round(n * 2) / 2# For example: 3.21 * 2 == 6.42 round(6.42) == 6 6 / 2 == 3 3.31 * 2 = 6.62 round(6.62) == 7 7 / 2 == 3.35 cheers, barneyb On Wed, Nov 25, 2009 at 11:13 AM, Che Vilnonis ch...@asitv.com wrote: I'm trying to create a ratings system that shows 1/2 values. Quick question, what would be the easiest way to round a value to the nearest .5 of a number? I'm trying to avoid multiple if/elses. i.e. 3.21 would round to 3 i.e. 3.31 would round to 3.5 i.e. 3.61 would round to 3.5 i.e. 3.91 would round to 4 Thanks, Che ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328687 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Re: Round to the nearest .5 value
Che Vilnonis wrote: Multiply number by 2, round it, divide it by 2. ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328688 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Round to the nearest .5 value
Barney Boisvert wrote: 7 / 2 == 3.35 No it doesn't! I suspect your typing thumbs, or is it your ring finger, is making you look like a bad mathematician! ;-) ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328690 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Round to the nearest .5 value
Sweet! Many years ago, in high school Algebra class, I would have been able to figure that out. It's amazing what we forget. -Original Message- From: Barney Boisvert [mailto:bboisv...@gmail.com] Sent: Wednesday, November 25, 2009 2:23 PM To: cf-talk Subject: Re: Round to the nearest .5 value Assuming 'n' is your number, use #round(n * 2) / 2# For example: 3.21 * 2 == 6.42 round(6.42) == 6 6 / 2 == 3 3.31 * 2 = 6.62 round(6.62) == 7 7 / 2 == 3.35 cheers, barneyb ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328691 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Round to the nearest .5 value
cfoutput #Round(3.21/0.5) *0.5# #Round(3.31/0.5) *0.5# #Round(3.61/0.5) *0.5# #Round(3.91/0.5) *0.5# #Round(4.24/0.5) *0.5# #Round(4.49/0.5) *0.5# #Round(4.50/0.5) *0.5# /cfoutput produces: 3 3.5 3.5 4 4 4.5 4.5 Cheers, Allen Che Vilnonis wrote: I'm trying to create a ratings system that shows 1/2 values. Quick question, what would be the easiest way to round a value to the nearest .5 of a number? I'm trying to avoid multiple if/elses. i.e. 3.21 would round to 3 i.e. 3.31 would round to 3.5 i.e. 3.61 would round to 3.5 i.e. 3.91 would round to 4 Thanks, Che ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328693 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Re: Round to the nearest .5 value
Oops, you're right, Ian. That should be 7 / 2 == 3.5 (remove the second three), of course. I wonder who had coffee this morning and therefore can't control his fine motor skills. At least with a computer your assertions are actually checked, so typos get caught. :) cheers, barneyb On Wed, Nov 25, 2009 at 11:32 AM, Ian Skinner h...@ilsweb.com wrote: Barney Boisvert wrote: 7 / 2 == 3.35 No it doesn't! I suspect your typing thumbs, or is it your ring finger, is making you look like a bad mathematician! ;-) -- Barney Boisvert bboisv...@gmail.com http://www.barneyb.com ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328694 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Round to the nearest .5 value
That is beautiful in form and simplicity. Bravo. On Wed, Nov 25, 2009 at 11:27 AM, Ian Skinner h...@ilsweb.com wrote: Multiply number by 2, round it, divide it by 2. ~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:328695 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4