<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 18, 2016 at 5:26 PM, Nicolas Cellier <span dir="ltr">&lt;<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div>Hi Tim,<br></div>you can&#39;t round float that easily, they are not multiple of 1/5.<br></div>But you can round their printed decimal representation.<br></div><div>That&#39;s fair because this is what you want: print the damn float<br></div><div><br></div>292.37 printShowingMaxDecimalPlaces: 1<br><div><div><div><div><div class="gmail_extra"><br><br></div><div class="gmail_extra">Technically, 2924*0.1 and 2924/10.0 differ.</div><div class="gmail_extra">The second one is guaranteed to be the closest float to (2924/10) because there&#39;s only one inexact operation involved (the division).<br></div><div class="gmail_extra">The first one, not so, it&#39;s 2924 * (1/10.0), two inexact operations<br>Remember, 1/10 = 0.1 is false...</div><div class="gmail_extra">That&#39;s why roundTo: is failing: it uses the second formula.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Otherwise, of course, you would use ScaledDecimals or Fractions...<br></div></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div class="gmail_extra"><br>239.37 roundTo: 0.1s.<br>(239.37 roundTo: 1/10) asFloat.<br><br></div><div class="gmail_extra">But you want to print rounded, so just print rounded, don&#39;t bother with round.<br></div></div></div></div></div></div></blockquote><div><br></div><div>or FixedDecimals (if you want to load that package). </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div></div></blockquote><div>    239.37 asFixedDecimal: 1 &quot;==&gt; 239.4 &quot;<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div class="gmail_extra"></div><div><div class="h5"><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_quote">2016-07-19 1:20 GMT+02:00 tim Rowledge <span dir="ltr">&lt;<a href="mailto:tim@rowledge.org" target="_blank">tim@rowledge.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">I know that it’s a hard thing to deal with floating point numbers around the fiddly edges, but are we really unable to do a decent job of rounding floats to reasonable numbers of decimal places? This can’t be a rare thing to need to do.<br>
<br>
For example, I’m reading temperature values for various places from the <a href="http://openweathermap.org" rel="noreferrer" target="_blank">openweathermap.org</a> site and many of them are specified to 2dp, which seems a bit optimistic for a weather site. But if I try, for example<br>
292.37 roundTo: 0.1<br>
I get the very unhelpful 292.40000…00003 (with too many 0s to count by eye). I understand that one option is to use formatted printing of the number but that doesn’t seem practical for this usage right now.<br>
<br>
<br>
So, numerics aficionados, can we do any better than the sensible seeming but unsuccessful code in Number&gt;&gt;roundTo: ?<br>
<br>
tim<br>
--<br>
tim Rowledge; <a href="mailto:tim@rowledge.org" target="_blank">tim@rowledge.org</a>; <a href="http://www.rowledge.org/tim" rel="noreferrer" target="_blank">http://www.rowledge.org/tim</a><br>
Ubi dubium ibi libertas<br>
<br>
<br>
</blockquote></div><br></div></div></div></div></div></div></div></div>
<br><br>
<br></blockquote></div><br></div></div>