<div class="gmail_quote">On Jan 5, 2008 5:54 PM, <<a href="mailto:glyph@divmod.com" target="_blank">glyph@divmod.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>At first I didn't realize why I'd missed this feature. While the<br></div>rounding *modes* are well documented, though, after 20 minutes of<br>reading documentation I still haven't found a method or function that
<br>simply rounds a decimal to a given significant digit. Is there one,<br>should there be one, or is the user simply meant to use Context.quantize<br>with appropriate arguments?<br><div><div></div><div></div>
</div></blockquote><div><br></div><div>quantize is about as close as it gets. Note that it's a Decimal method as well as a Context method, so you can invoke it directly on a given decimal:
</div><div><br></div><div><div>>>> Decimal("2.34567").quantize(Decimal("0.01"))</div><div>Decimal("2.35")</div><div><br>
</div><div>I've also occasionally felt a need for a simple rounding function that isn't affected by context. Would others be interested in such a function being added to Decimal? I guess there are two possibly useful operations: (1) round to a particular decimal place (
e.g. nearest ten, nearest hundredth, ..) and (2) to round to a particular number of significant digits; in both cases, the user should be able to specify the desired rounding mode. And for each operation, it might also be useful to specify whether the result should be padded with zeros to the desired length or not. (
i.e. when rounding 3.399 to 3 significant places, should it produce 3.4 or 3.40?)<br></div><div><br class="webkit-block-placeholder"></div><div>Any thoughts?</div><div><br class="webkit-block-placeholder"></div><div>Mark</div>
</div></div>