<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — Rounding error in includes/ui/ui_view.inc -> price_in_words()]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=2800&amp;type=atom" />
	<updated>2012-02-08T13:51:08Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=2800</id>
		<entry>
			<title type="html"><![CDATA[Re: Rounding error in includes/ui/ui_view.inc -> price_in_words()]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=11758#p11758" />
			<content type="html"><![CDATA[<p>Hi Joe,</p><div class="quotebox"><cite>joe wrote:</cite><blockquote><p>Due to a bug in sprintf replacing the if statement solves the problem:</p></blockquote></div><p>Thanks for the new patch, it seems to work well for me!&nbsp; And I don&#039;t think there&#039;s a bug in sprintf(), it&#039;s just a known problem that PHP (and other languages like Python) don&#039;t deliver proper math results by default when dealing with base 10 floating point numbers.&nbsp; The real fix would be to use something like BC Math here which is slower (not enough to matter for this purpose) but delivers correct results all the time.&nbsp; We ended up using BC Math for some of our custom FA reports because otherwise it&#039;s really flakey to do math on floats and then try to do an equality comparison with 0.0, for example.&nbsp; Because the result of your floating point math will actually be something like 0.00000000000001239458345 if you printf() it with enough digits to see that.&nbsp; Annoying!</p><p>-Jason</p>]]></content>
			<author>
				<name><![CDATA[maasj]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=377</uri>
			</author>
			<updated>2012-02-08T13:51:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=11758#p11758</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Rounding error in includes/ui/ui_view.inc -> price_in_words()]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=11755#p11755" />
			<content type="html"><![CDATA[<p>Due to a bug in sprintf replacing the if statement solves the problem:<br /></p><div class="codebox"><pre><code>    if ($dec &gt; 0)
    {
        $divisor = pow(10, $dec);
        $frac = round2($amount - floor($amount), $dec) * $divisor;
        $frac = sprintf(&quot;%0{$dec}d&quot;, round2($frac, 0));
        $and = _(&quot;and&quot;);
        $frac = &quot; $and $frac/$divisor&quot;;
    }</code></pre></div><p>The HG repository will be updated.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2012-02-07T23:38:23Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=11755#p11755</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Rounding error in includes/ui/ui_view.inc -> price_in_words()]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=11754#p11754" />
			<content type="html"><![CDATA[<p>Thanks for this patch. But for the amount of 845.14 your algorithm prints Eight hundred and Fourty_five and 13/100.<br />With the old algorithm it was printed ok. So we will have to improve the algorithm a bit.</p><p>/Joe</p>]]></content>
			<author>
				<name><![CDATA[joe]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=3</uri>
			</author>
			<updated>2012-02-07T22:22:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=11754#p11754</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Rounding error in includes/ui/ui_view.inc -> price_in_words()]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=11751#p11751" />
			<content type="html"><![CDATA[<p>Hello,</p><p>Calling price_in_words() on the amount 1960.58 with user_price_dec set to 2 produces the string &#039;&#039;One Thousand Nine Hundred and Sixty and 57/100&#039;&#039;&nbsp; &lt;-- Note &#039;&#039;57/100&#039;&#039; which should be &#039;&#039;58/100&#039;&#039;.</p><p>Also, the amount 16418.29 produces &#039;&#039;Sixteen Thousand Four Hundred and Eighteen and 28/100&#039;&#039;.&nbsp; Again, note &#039;&#039;28/100&#039;&#039; instead of &#039;&#039;29/100&#039;&#039;.</p><p>The following patch to ui_view.inc seems to fix the problem for me, but I&#039;m not sure if it&#039;s the best fix.&nbsp; Can you think of a better one?</p><div class="codebox"><pre><code>--- ui_view-orig.inc    2012-02-07 13:24:33.398225655 -0500
+++ ui_view.inc    2012-02-07 13:24:36.218220917 -0500
@@ -774,7 +774,7 @@
     if ($dec &gt; 0)
     {
         $divisor = pow(10, $dec);    
-        $frac = round2($amount - floor($amount), $dec) * $divisor;
+        $frac = (int) round2(($amount - floor($amount)) * $divisor, $dec);
         $frac = sprintf(&quot;%0{$dec}d&quot;, $frac);
         $and = _(&quot;and&quot;);
         $frac = &quot; $and $frac/$divisor&quot;;</code></pre></div>]]></content>
			<author>
				<name><![CDATA[maasj]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=377</uri>
			</author>
			<updated>2012-02-07T18:35:47Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=11751#p11751</id>
		</entry>
</feed>
