<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2658.2">
<TITLE>RE: round() to nearest .05 ?</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>[tertius]</FONT>
</P>

<P><FONT SIZE=2>#- I'm trying to round my float total to the nearest .05 cents.</FONT>
<BR><FONT SIZE=2>#- </FONT>
<BR><FONT SIZE=2>#- 12.01    should produce 12.00</FONT>
<BR><FONT SIZE=2>#-   0.14    should produce 0.10</FONT>
<BR><FONT SIZE=2>#-   2.28          "        2.25</FONT>
<BR><FONT SIZE=2>#- 703.81         "        703.80</FONT>
<BR><FONT SIZE=2>#- </FONT>
<BR><FONT SIZE=2>#- "%.02f"%100.0099 produces 100.01 (which I know is right)</FONT>
<BR><FONT SIZE=2>#- No combination of round and/or "%.02f" works for me.</FONT>
</P>

<P><FONT SIZE=2>Don't understand. Shouldn't 0.14 produce 0.15? And 2.28, 2.30?</FONT>
</P>
<BR>

<P><FONT SIZE=2>#- What's the best way to get there? Should I write a function </FONT>
<BR><FONT SIZE=2>#- to manage my </FONT>
<BR><FONT SIZE=2>#- rounding or is there a simpler/better way?</FONT>
</P>

<P><FONT SIZE=2>For this kind of math, don't use float, use Decimal:</FONT>
</P>

<P><FONT SIZE=2>    <A HREF="http://www.python.org/dev/doc/devel/lib/module-decimal.html" TARGET="_blank">http://www.python.org/dev/doc/devel/lib/module-decimal.html</A></FONT>
</P>

<P><FONT SIZE=2>You'll have exact accuracy and a lot of rounding modes.</FONT>
</P>

<P><FONT SIZE=2>If you can not upgrade to Py2.4 yet, but have 2.3.x, install the Decimal package from <A HREF="http://sf.net/projects/sigefi/" TARGET="_blank">http://sf.net/projects/sigefi/</A></FONT>
</P>

<P><FONT SIZE=2>.       Facundo</FONT>
</P>

</BODY>
</HTML>