<div dir="ltr">Yes, that is similar to what the Decimal library does. It maintains a precision value and a scaled integer.<div><br></div><div>So <span style="font-size:12.8px">€ 10.02 could be represented as:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Decimal 2 1002</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">It also has nice functions for dividing out currency into equal parts, or ratios, while preserving the property that the total of the parts should add up to the original value.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I just thought it might be interesting to encode the precision in the type, and just use an integer in the representation. Then it's even more efficient at runtime. Maybe I'll write my own library. Or perhaps there's another one that does that already.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The Data.Number.Fixed library encodes the precision in the type, but it uses rational number for the values, and only guarantees the precision within an epsilon range, so that "two digits of precision" means it could be +/- .01, which is not good for currency.</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 5, 2016 at 12:26 AM, Imants Cekusins <span dir="ltr"><<a href="mailto:imantc@gmail.com" target="_blank">imantc@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><p dir="ltr">> I prefer the Decimal library for currency.</p>
</span><p dir="ltr">here is another option to consider:</p>
<p dir="ltr">data Cur = Eur | ...<br>
data Money = Mint Cur Int | Mtup Cur (Int,Int)</p>
<p dir="ltr">precision::Cur -> Int<br>
precision Eur = 2<br>
precision _ = 2</p>
<p dir="ltr">€ 10.02 would be:<br>
Mint Eur 1002<br>
Mtup Eur (10,2)</p>
<p dir="ltr">This adds overhead to computations. However precision and correct currency is guaranteed.<br>
</p>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>