January 18, 2006

NEXT POST
Rounding to a given number <p>These custom functions round a number &ldquo;naturally&rdquo;. For example, they can be used to round time to 15 minutes, or money to $.25, or just any number to a multiple of another number.</p> <p>All the functions have the same syntax:</p> <p><code>Round To( <var>number</var>, <var>precision</var> )</code></p> <p><code>Round Down To( <var>number</var>, <var>precision</var> )</code></p> <p><code>Round Up To( <var>number</var>, <var>precision</var> )</code></p> <p>where <var>number</var> is the number to round and <var>precision</var> is the number to calculate the appropriate multiple of. For example, <code>Round To( 13, 5 )</code> rounds 13 to the nearest multiple of 5, i.e. 15. The <code>Round Up To()</code> and <code>Round Down To()</code> round the <var>number</var> to higher or lower multiple of <var>precision</var> respectively.</p> <p>Functions themselves are very simple:</p> <p>Round To( number, precision )</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#3300cc;">Round</span>( number <span style="color:#996699;">/</span> precision; 0 ) * precision</pre> <p>Round Down To( number, precision )</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#3300cc;">Floor</span>( number <span style="color:#996699;">/</span> precision; 0 ) * precision</pre> <p>Round Up To( number, precision )</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#3300cc;">Ceiling</span>( number <span style="color:#996699;">/</span> precision; 0 ) * precision</pre> <h4>How to round time</h4> <p>Don't calculate the number of seconds: use the <code>Time()</code> function instead. For example, to round to an hour:</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#3300cc;">Round To( Time Field, <span style="color:#3300cc;">Time</span>( 1, 0, 0 ) )</pre> <p>to 15 minutes:</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#3300cc;">Round To( Time Field, <span style="color:#3300cc;">Time</span>( 0, 15, 0 ) )</pre> <p>You might also want to make a few <a href="http://edoshin.typepad.com/bits_and_pieces/2005/10/constant_custom.html">constant custom functions</a>: <code>HOURS</code> and <code>MINUTES</code>. These function must return <code>Time( 1, 0, 0 )</code>, <code>Time( 0, 1, 0 )</code> respectively. (You could make a function for seconds, if you need them, but you'll need to select a name carefully, because Seconds is taken already.) With such functions your code will be more readable, like this:</p> <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Round To( Time Field, 2 * HOURS ) Round To( Time Field, 0.5 * HOURS ) Round To( Time Field, 15 * MINUTES ) Round To( Time Field, 1/2 * MINUTES ) </pre> <p>There's yet another good use of this function: it can help to approximate a number to a common fraction. I'll write about this later.</p>

m.edoshin

I'm a FileMaker developer with 3 kids.

The Typepad Team

My Other Accounts

Recent Comments