<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to handle rounding error in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19755#M18001</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Another thing I thought of was to convert to string and use Regex.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 01 Aug 2016 20:06:39 GMT</pubDate>
    <dc:creator>Phil_Brown</dc:creator>
    <dc:date>2016-08-01T20:06:39Z</dc:date>
    <item>
      <title>How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19750#M17996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have this script which does a simple calculation and displays result in a window:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;subtotal = &lt;SPAN style="color: #009193;"&gt;100.05&lt;/SPAN&gt;;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;discountPercent = &lt;SPAN style="color: #009193;"&gt;0.1&lt;/SPAN&gt;;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;discountAmount = subtotal * discountPercent;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;totalBeforeTax = subtotal - discountAmount;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;formattedMessage = &lt;SPAN style="color: #032ce4;"&gt;Concat&lt;/SPAN&gt;(&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #942193;"&gt;&lt;SPAN style="color: #000000;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;"Subtotal:"&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Repeat&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;" "&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #009193;"&gt;5&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Format&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(subtotal, &lt;/SPAN&gt;"Currency"&lt;SPAN style="color: #000000;"&gt;),&amp;nbsp; &lt;/SPAN&gt;"\!n"&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #942193;"&gt;&lt;SPAN style="color: #000000;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;"Discount Percent:"&lt;SPAN style="color: #000000;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Repeat&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;" "&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #009193;"&gt;5&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Format&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(discountPercent, &lt;/SPAN&gt;"Percent"&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;"\!n"&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #942193;"&gt;&lt;SPAN style="color: #000000;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;"Discount Amount:"&lt;SPAN style="color: #000000;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Repeat&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;" "&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #009193;"&gt;5&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Format&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(discountAmount, &lt;/SPAN&gt;"Currency"&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;"\!n"&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #942193;"&gt;&lt;SPAN style="color: #000000;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;"Total before tax:"&lt;SPAN style="color: #000000;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Repeat&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(&lt;/SPAN&gt;" "&lt;SPAN style="color: #000000;"&gt;,&lt;/SPAN&gt;&lt;SPAN style="color: #009193;"&gt;5&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Format&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(totalBeforeTax, &lt;/SPAN&gt;"Currency"&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;"\!n"&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;);&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;formattedMessageDisplay = &lt;SPAN style="color: #032ce4;"&gt;New Window&lt;/SPAN&gt;( &lt;SPAN style="color: #942193;"&gt;"Result"&lt;/SPAN&gt;,&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Lineup Box&lt;/SPAN&gt;( &lt;SPAN style="color: #032ce4;"&gt;N Col&lt;/SPAN&gt;( &lt;SPAN style="color: #009193;"&gt;1&lt;/SPAN&gt; ), Spacing( &lt;SPAN style="color: #009193;"&gt;0&lt;/SPAN&gt;, &lt;SPAN style="color: #009193;"&gt;0&lt;/SPAN&gt; ), &lt;SPAN style="color: #032ce4;"&gt;Border Box&lt;/SPAN&gt;( &lt;SPAN style="color: #032ce4;"&gt;Left&lt;/SPAN&gt;( &lt;SPAN style="color: #009193;"&gt;20&lt;/SPAN&gt; ), &lt;SPAN style="color: #032ce4;"&gt;Right&lt;/SPAN&gt;( &lt;SPAN style="color: #009193;"&gt;20&lt;/SPAN&gt; ), top( &lt;SPAN style="color: #009193;"&gt;20&lt;/SPAN&gt; ), bottom( &lt;SPAN style="color: #009193;"&gt;20&lt;/SPAN&gt; ),&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Text Box&lt;/SPAN&gt;( formattedMessage, &amp;lt;&amp;lt;&lt;SPAN style="color: #011993;"&gt;SetFontSize&lt;/SPAN&gt;( &lt;SPAN style="color: #009193;"&gt;14&lt;/SPAN&gt; ) ) )&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;),&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;SPAN class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/SPAN&gt;&lt;SPAN style="color: #032ce4;"&gt;Show&lt;/SPAN&gt;(subtotal, discountAmount, totalBeforeTax)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;);&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;It produces the following, in the log and the output window :&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;&lt;IMG /&gt;&lt;IMG style="font-size: 10pt;" /&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="12109_roundingerror.png" style="width: 493px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/3456i3BDD1EFD692C202B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="12109_roundingerror.png" alt="12109_roundingerror.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;&lt;/P&gt;&lt;P style="margin: 0px; line-height: normal;"&gt;So using &lt;SPAN style="color: #032ce4;"&gt;Format&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;(totalBeforeTax, &lt;/SPAN&gt;&lt;SPAN style="color: #942193; font-family: Menlo; font-size: 11px;"&gt;"Currency"&lt;/SPAN&gt;&lt;SPAN style="color: #000000;"&gt;), &lt;/SPAN&gt;results&lt;SPAN style="font-size: 10pt;"&gt; in totalBeforeTax being "rounded up". I can think of a variety of ways of handling this, but wondered what would be most recommended.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Oct 2016 03:32:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19750#M17996</guid>
      <dc:creator>Phil_Brown</dc:creator>
      <dc:date>2016-10-19T03:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19751#M17997</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't do a lot of financial reporting, however, when I need to worry about rounding errors, I just use the functions Round(),Ceiling() and Floor().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 31 Jul 2016 12:35:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19751#M17997</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2016-07-31T12:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19752#M17998</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jim,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for you reply. So, Floor and Ceiling round to the nearest integer. Format (x, "Currency") basically appears to use Round(x,2). One would think the "Currency" format would employ a truncation method rather than rounding. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 01:09:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19752#M17998</guid>
      <dc:creator>Phil_Brown</dc:creator>
      <dc:date>2016-08-01T01:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19753#M17999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You might want to round/floor/ceiling the discountAmount calculation.&amp;nbsp; Round has a second argument that you might want to set to 2 for digits after the decimal.&amp;nbsp; Floor and ceiling will need something like floor(x*100)/100 to get a similar result.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt; font-family: 'courier new', courier;"&gt;subtotal = 100.05;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 8pt;"&gt;discountPercent = 0.1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 8pt;"&gt;discountAmount = &lt;STRONG style="font-size: 10pt;"&gt;round&lt;/STRONG&gt;(subtotal * discountPercent,&lt;SPAN style="font-size: 10pt;"&gt;&lt;STRONG&gt;2)&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 8pt;"&gt;totalBeforeTax = subtotal - discountAmount;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt; font-family: 'courier new', courier;"&gt;show(discountAmount, totalBeforeTax);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;discountAmount = 10.01;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;totalBeforeTax = 90.04;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The format function in your original code is on the knife-edge of rounding up or down with the 0.005 rounded to two places.&amp;nbsp; The discrepancy in the presentation is triggered by the value having a minus in one case and a plus in the other...both values are rounded up, but one of them has negative discountAmount in the calculation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Floating point numbers used in JMP represent numbers like 0.01 as a truncated repeating binary fraction.&amp;nbsp; Just like 1/3 isn't exactly 0.333333333333, the internal representation isn't exact (except for binary fractions like 1/2, 1/4, 1/8, ...).&amp;nbsp; if you add up 100 pennies, represented as 0.01, you'll get a number that is very close to 1.00, but different in the 16'th decimal place.&amp;nbsp; This very tiny error has always made accounting packages choose another representation: &lt;A href="http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency" title="http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency"&gt;java - Why not use Double or Float to represent currency? - Stack Overflow&lt;/A&gt;​&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt; font-family: 'courier new', courier;"&gt;total = 0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 8pt;"&gt;For( i = 1, i &amp;lt;= 100, i++, total += .01 );&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt; font-family: 'courier new', courier;"&gt;show(&amp;nbsp; format(total,"fixed",20,16) );&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Format(total, "fixed", 20, 16) = "1.0000000000000007";&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 15:07:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19753#M17999</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2016-08-01T15:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19754#M18000</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Craige,&amp;nbsp; indeed I've been considering building an interface to allow JMP to access java's BigDecimal class. Are there any plans to include this as a type in the next version of JMP?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 18:54:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19754#M18000</guid>
      <dc:creator>Phil_Brown</dc:creator>
      <dc:date>2016-08-01T18:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19755#M18001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Another thing I thought of was to convert to string and use Regex.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 20:06:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19755#M18001</guid>
      <dc:creator>Phil_Brown</dc:creator>
      <dc:date>2016-08-01T20:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle rounding error</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19756#M18002</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No Big Number plans at this time.&amp;nbsp; There were a lot of interesting points being made in the StackOverflow link.&amp;nbsp; Different solutions might be appropriate depending on what you'll do with the numbers.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Craige&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 01 Aug 2016 20:30:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-handle-rounding-error/m-p/19756#M18002</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2016-08-01T20:30:57Z</dc:date>
    </item>
  </channel>
</rss>

