YOUR FEEDBACK
johnpetersen wrote: Great post. You hit some good points, and hopefully me sending this post. It wil...
Cloud Computing Conference
November 19-21 San Jose, CA
Register Today and SAVE !..


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Playing with Numbers
It's all in the math

For this month's installment, I thought I'd return the Community column to its original roots ("Tales From the List") and focus on a recent thread in the CFDJ List. Brian Simpson wrote to the List posing the following problem: "How do I round to the nearest 'x'?" Specifically, Brian has a client that wants to be able to use its e-commerce application to reduce all of their product prices by 10% and then round up to the nearest number evenly divisible by 50. Before you read on, ask yourself how you would achieve this task. It's not as easy as you'd think...or is it?

The first response Brian received was from Evik. He suggested removing any decimal values (cents) and then treating the number as a string. If the last two characters have a numeric value in the range from 1-50, then make the last two characters 50. If the last two characters have a numeric value in the range 51-99, then make the last two numbers 00 and then add 100 to the number. The code, written in a <CFSCRIPT> function, to do this would look like the following:


<cfscript>
   function roundToNext50(theNum){
      var retVal = 0;
      var someNum = 0;

      if (len(theNum) is 1){
         retVal = 50;
      } else {
         someNum = val(right(theNum, 2));
      }

      if (not retVal){
         if (someNum gt 50){
            if (len(theNum) is 2){
               retVal = 100;
            } else {
               retVal = 100 + val(left(theNum, len(theNum) - 2) & "00");
            }
         } else {
            if (right(theNum, 2)){
               retVal = val(left(theNum, len(theNum) - 2) & "50");
            } else {
               retVal = theNum;
            }
         }
      }
      return retVal;
   }
</cfscript>

This is not terribly pretty code - I certainly wouldn't want to have to make this function flexible enough to support rounding up to a dynamic increment. That said, it will round any number not already divisible by 50 to the next number that is divisible by 50. There are plenty of other ways to write the code to treat a number as a string and round it to the next 50 - this is just one of them.

A regular on the CFDJ List, Dave (from TheaterMania), then responded with an elegant solution that surprised everyone. He simply states, "What I do is divide by the rounding increment, round up to the nearest integer, then multiply by the rounding increment." So you divide your number by 50, use ceiling to round it up, then multiply by 50. Presto! You have your next number divisible by 50. The code for this, written in a <CFSCRIPT> function, would look like this:


<cfscript>
   function roundToNext50(theNum){
      return ceiling(theNum/50)*50;
   }
</cfscript>

Quite a difference in the amount of code between the two, isn't there? What's more, this last technique is extremely flexible - add a second argument and you can now round to the next highest number evenly divisible by any increment you want, like so:


<cfscript>
   function roundToNextNum(theNum,theIncrement){
      return ceiling(theNum/theIncrement)*theIncrement;
   }
</cfscript>

I chose to discuss this thread because it illustrates a very important concept. Not just an algebraic concept, but also a development concept. Anytime your immediate solution to a business logic problem involves treating a number as a string, stop and ask yourself whether or not there's a way to achieve your objective using math functions. There almost always is - and it's almost always more elegant. Note that I said business logic problem. Treating a number as a string in presentation-tier code is certainly a safe (and sometimes necessary) practice. If your math skills aren't up to snuff or you're unsure how to write an algorithm to achieve the results you want, send an e-mail to one of the list servers - you're almost always sure to get decent help.

About Simon Horwith
Simon Horwith is the editor-in-chief of ColdFusion Developer's Journal and is the CIO at AboutWeb, LLC, a Washington, DC based company specializing in staff augmentation, consulting, and training. Simon is a Macromedia Certified Master Instructor and is a member of Team Macromedia. He has been using ColdFusion since version 1.5 and specializes in ColdFusion application architecture, including architecting applications that integrate with Java, Flash, Flex, and a myriad of other technologies. In addition to presenting at CFUGs and conferences around the world, he has also been a contributing author of several books and technical papers. You can read his blog at http://simon.coldfusionjournal.com.

CFDJ LATEST STORIES . . .
Adobe and ARM are gonna put Flash Player 10 and AIR, the stuff of web video and rich Internet apps, on ARM widgets by the second half of next year. They mean phones, set-tops, MIDs, TVs, car mojo and personal media devices, which have so far only had access to Flash Lite, not the best ...
Of all domestic air carriers, I like Continental the most. They showed Mamma Mia and the food was bearable. Last month, I was in the air for 14 hours flying to Japan, and now the trip across the USA is a piece of cake. I have only carry luggage with me. This small bag has all the cloth...
I’ll just give you one example. Last week my colleague and I were running a private Flex workshop for software architects of a large corporation who are about to start development with Flex. Needless to say that they are smart and experienced software professionals. Some of them alre...
A round-up of the many themes and topics of interest to infrastructure architects, developers and IT managers featuring at SYS-CON's Cloud Computing Expo being held November 19-21, 2008 at The Fairmont Hotel in San Jose, California. The conference is expecting a record turnout of senio...
AIR adds to Flex has a pretty straightforward API for working with local files and directories. There is a simple mechanism of installing and upgrading AIR applications. If you want, you can digitally sign them too. AIR 1.5 introduces local encryption, which means that you can encrypt...
Reading conference speaker's agreements may reveal some interesting gems. Since I don't have a PR agent, I have to make the following public statement by myself: "I'm not going to damage anyone's reputation (including developers of PureMVC framework) for abuse of design patterns. I'm r...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

MOST READ THIS WEEK
ADS BY GOOGLE