My Five Cents Worth

by Rod Martin - GUI Computing

Recently I've been working on a business application that generates recommended retail prices for products. Each price had to be rounded to the nearest 5 cents, as per the table below :

PriceRounded to 5 Cents
$10.00 $10.00
$10.01 $10.00
$10.02 $10.00
$10.03 $10.05
$10.04 $10.05
$10.05 $10.05
$10.06 $10.05
$10.07 $10.05
$10.08 $10.10
$10.09 $10.10

I consulted the VB Help file to see if there was a function which would apply the conversion formula I needed. I couldn't find anything, so I wrote my own. This isn't the most complicated piece of code you've ever seen, but hopefully this will save you some time working it out yourself in the future. (Note that this also converts numbers of more than two decimal places, i.e. 10.2255 becomes 10.25 and 10.22444 becomes 10.20).

Because the speed at which this calculation was performed was important to my program (it was calling this function up to a million times on each run) I pulled this code out of the function, which reduced the time by more than 10%. If speed isn't that important for your app, it's obviously more convenient to have this formula nicely wrapped in a function. But if you can think of way to speed this up, let me know.


Written by: Rod Martin
August 1995

Image of arrow to previous article Image of arrow to next article

[HOME] [TABLE OF CONTENTS] [SEARCH]