Rounding Values with the Round Function

I l @ ve RuBoard

When you find it necessary to round numbers in your application, you'll be happy to know you don't have to do it manually; ColdFusion provides the round function to standardize numbers to the closest integer (whole number).

When might you need to do this? Suppose your company has decided to change all their prices to whole-dollar amounts, so instead of $14.95 the price needs to be $15.00. With the database you have (or pretty much any other one, for that matter), it would be easy to go in and change the individual prices, but if you had thousands of items you might quickly come to think it wasn't so easy after all. By using Round you can implement a quick, short- term fix while the database is being updated.

TIP

Unfortunately, ColdFusion only rounds to whole numbers. So, if you had a number like 14.2354 and you wanted to round to two decimal places, you would first need to multiply by 100, round, and then divide the number by 100, thus yielding 14.24. You could also use the numberformat () function to round to a decimal place.


  1. Open X:\Lesson9\Start\productinfo.cfm.

    This is the same template you have used throughout to display the product information.

  2. Add the Round function around the #ProductPrice# variable in the display area on line 114.

    This will cause the prices, when displayed, to be rounded up or down to the nearest whole number. This will not change the price in the database, just the value when it is displayed.

    NOTE

    You will need to use the Round function anywhere the ProductPrice variable is referenced. That includes any time the variable is set or output.

  3. Save the file in the Lesson7\Start folder in the Inetpub\ wwwroot directory.

    Once again, it's OK that you are overwriting the old file.

  4. Open a Web browser and type http://127.0.0.1/Lesson7/Start/productinfo.cfm into the address window.

    All the prices for all the items should appear as whole dollar amounts. They still have the 00 after them because this is inserted by the DollarFormat function. If this function were not present, the amount would be a single integer without the decimal places.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net