<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.opentaps.org/docs/index.php?action=history&amp;feed=atom&amp;title=How_to_Use_Java_BigDecimal%3A_A_Tutorial</id>
	<title>How to Use Java BigDecimal: A Tutorial - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://docs.opentaps.org/docs/index.php?action=history&amp;feed=atom&amp;title=How_to_Use_Java_BigDecimal%3A_A_Tutorial"/>
	<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;action=history"/>
	<updated>2026-08-26T01:16:31Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.1</generator>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5735&amp;oldid=prev</id>
		<title>Sichen: /* Immutability and Arithmetic */</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5735&amp;oldid=prev"/>
		<updated>2010-04-09T20:58:56Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Immutability and Arithmetic&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 20:58, 9 April 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l149&quot; &gt;Line 149:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 149:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   amount.add( thisAmount );&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   amount.add( thisAmount );&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;THIS IS THE MOST COMMON MISTAKE MADE WITH BIGDECIMALS!&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;'''&lt;/ins&gt;THIS IS THE MOST COMMON MISTAKE MADE WITH BIGDECIMALS&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;'''&lt;/ins&gt;!&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Comparison===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Comparison===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Sichen</name></author>
		
	</entry>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5731&amp;oldid=prev</id>
		<title>Sichen: /* Introducing BigDecimal */</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5731&amp;oldid=prev"/>
		<updated>2010-04-08T19:29:34Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Introducing BigDecimal&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 19:29, 8 April 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l49&quot; &gt;Line 49:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 49:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;two things:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;two things:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &lt;del class=&quot;diffchange diffchange-inline&quot;&gt; &lt;/del&gt;Ability to specify a scale, which represents the number of digits after  &lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# Ability to specify a scale, which represents the number of digits after the decimal place&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;    &lt;/del&gt;the decimal place&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#  Ability to specify a rounding method&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#  Ability to specify a rounding method&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The java.math.BigDecimal class handles both of these considerations.  See [http://java.sun.com/j2se/1.&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;4&lt;/del&gt;.&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;2&lt;/del&gt;/docs/api/java/math/BigDecimal.html]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The java.math.BigDecimal class handles both of these considerations.  See [http://java.sun.com/j2se/1.&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;5&lt;/ins&gt;.&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;0&lt;/ins&gt;/docs/api/java/math/BigDecimal.html &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;BigDecimal Javadocs&lt;/ins&gt;]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Creating a big decimal from a (scalar) double is simple:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Creating a big decimal from a (scalar) double is simple:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l69&quot; &gt;Line 69:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 68:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   bd = new BigDecimal(1.5);&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   bd = new BigDecimal(1.5);&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   bd.toString(); // =&amp;gt; 0.1499999999999999944488848768742172978818416595458984375&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;   bd.toString(); // =&amp;gt; 0.1499999999999999944488848768742172978818416595458984375&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Rounding and Scalin===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Rounding and Scalin===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Sichen</name></author>
		
	</entry>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5730&amp;oldid=prev</id>
		<title>Sichen at 19:24, 8 April 2010</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5730&amp;oldid=prev"/>
		<updated>2010-04-08T19:24:56Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 19:24, 8 April 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;__TOC__&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===The Problem===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===The Problem===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Sichen</name></author>
		
	</entry>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5729&amp;oldid=prev</id>
		<title>Sichen: Protected &quot;How to Use Java BigDecimal: A Tutorial&quot;: Sysop page [edit=sysop:move=sysop]</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5729&amp;oldid=prev"/>
		<updated>2010-04-08T19:24:36Z</updated>

		<summary type="html">&lt;p&gt;Protected &amp;quot;&lt;a href=&quot;/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&quot; title=&quot;How to Use Java BigDecimal: A Tutorial&quot;&gt;How to Use Java BigDecimal: A Tutorial&lt;/a&gt;&amp;quot;: Sysop page [edit=sysop:move=sysop]&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 19:24, 8 April 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Sichen</name></author>
		
	</entry>
	<entry>
		<id>https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5728&amp;oldid=prev</id>
		<title>Sichen: New page: ===The Problem===  When we started building the General Ledger services for accounting, we  discovered that there were errors of 0.01 cent or more in many places.  This made accounting pra...</title>
		<link rel="alternate" type="text/html" href="https://docs.opentaps.org/docs/index.php?title=How_to_Use_Java_BigDecimal:_A_Tutorial&amp;diff=5728&amp;oldid=prev"/>
		<updated>2010-04-08T19:24:35Z</updated>

		<summary type="html">&lt;p&gt;New page: ===The Problem===  When we started building the General Ledger services for accounting, we  discovered that there were errors of 0.01 cent or more in many places.  This made accounting pra...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;===The Problem===&lt;br /&gt;
&lt;br /&gt;
When we started building the General Ledger services for accounting, we  discovered that there were errors of 0.01 cent or more in many places.  This made accounting practically impossible.  Who would want to bill a customer for $4.01 when his order says $4.00?  &lt;br /&gt;
&lt;br /&gt;
The reasons for these errors soon became clear:  Computations that yielded amounts, quantities, adjustments, and many other things were generally done with little or no attention to the special precision and rounding concerns that arise when dealing with financial issues.  &lt;br /&gt;
&lt;br /&gt;
All of these computations used Java doubles, which offer no way to control how the number is rounded or to limit the precision in computation.  We came up with a solution involving the use of [http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html java.math.BigDecimal], which gives us this control.  &lt;br /&gt;
&lt;br /&gt;
This document serves as a primer on financial math issues and as a tutorial on the use of BigDecimal in general &lt;br /&gt;
&lt;br /&gt;
===Primer on Financial Issues===&lt;br /&gt;
&lt;br /&gt;
Currency calculations require precision to a specific degree, such as two  digits after the decimal for most currencies.  They also require a specific  type of rounding behavior, such as always rounding up in the case of taxes.&lt;br /&gt;
&lt;br /&gt;
For example, suppose we have a product which costs 10.00 in a given currency and the local sales tax is 0.0825, or 8.25%. If we work it out on paper, the tax amount is,&lt;br /&gt;
&lt;br /&gt;
  10.00 * 0.0825 = 0.825&lt;br /&gt;
&lt;br /&gt;
Because our precision for the currency is two digits after the decimal, we need to round the 0.825 figure.  Also, because this is a tax, it is good practice to always round up to the next highest cent.  That way when the accounts are balanced at the end of the day, we never find ourselves underpaying taxes.&lt;br /&gt;
&lt;br /&gt;
  0.825 -&amp;gt; 0.83&lt;br /&gt;
&lt;br /&gt;
And so the total we charge to the customer is 10.83 in the local currency and pay 0.83 to the tax collector.  Note that if we sold 1000 of these, we would have overpaid the collector by this much,&lt;br /&gt;
&lt;br /&gt;
  1000 * (0.83 - 0.825) = 5.00&lt;br /&gt;
&lt;br /&gt;
Another important issue is where to do the rounding in a given computation. Suppose we sold Liquid Nitrogen at 0.528361 per liter.  A customer comes in  and buys 100.00 liters, so we write out the total price,&lt;br /&gt;
&lt;br /&gt;
  100.0 * 0.528361 = 52.8361&lt;br /&gt;
&lt;br /&gt;
Because this isn't a tax, we can round this either up or down at our discretion.  Suppose we round according to standard rounding rules: If the next significant digit is less than 5, then round down. Otherwise round up.&lt;br /&gt;
This gives us a figure of 52.84 for the final price.&lt;br /&gt;
&lt;br /&gt;
Now suppose we want to give a promotional discount of 5% off the entire  purchase.  Do we apply this discount on the 52.8361 figure or the 52.84 figure?  What's the difference?&lt;br /&gt;
&lt;br /&gt;
  Calculation 1: 52.8361 * 0.95 = 50.194295 = 50.19&lt;br /&gt;
  Calculation 2: 52.84 * 0.95 = 50.198 = 50.20&lt;br /&gt;
&lt;br /&gt;
Note that we rounded the final figure by using the standard rounding rule.  &lt;br /&gt;
&lt;br /&gt;
See how there's a difference of one cent between the two figures?  The old code never bothered to consider rounding, so it always did computations as in Calculation 1.  But in the new code we always round before applying promotions, taxes, and so on, just like in Calculation 2. This is one of the main reasons&lt;br /&gt;
for the one cent error.&lt;br /&gt;
&lt;br /&gt;
===Introducing BigDecimal===&lt;br /&gt;
&lt;br /&gt;
From the examples in the previous section, it should be clear that we need &lt;br /&gt;
two things:&lt;br /&gt;
&lt;br /&gt;
#  Ability to specify a scale, which represents the number of digits after &lt;br /&gt;
    the decimal place&lt;br /&gt;
#  Ability to specify a rounding method&lt;br /&gt;
&lt;br /&gt;
The java.math.BigDecimal class handles both of these considerations.  See [http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html]&lt;br /&gt;
&lt;br /&gt;
Creating a big decimal from a (scalar) double is simple:&lt;br /&gt;
&lt;br /&gt;
  bd = new BigDecimal(1.0);&lt;br /&gt;
&lt;br /&gt;
To get a BigDecimal from a Double, get its doubleValue() first.&lt;br /&gt;
&lt;br /&gt;
However it is a good idea to use the string constructor:&lt;br /&gt;
&lt;br /&gt;
  bd = new BigDecimal(&amp;quot;1.5&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
If you don't, then you'll get the following,&lt;br /&gt;
&lt;br /&gt;
  bd = new BigDecimal(1.5);&lt;br /&gt;
  bd.toString(); // =&amp;gt; 0.1499999999999999944488848768742172978818416595458984375&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Rounding and Scalin===&lt;br /&gt;
&lt;br /&gt;
To set the number of digits after the decimal, use the .setScale(scale) method.&lt;br /&gt;
However, it is good practice to also specify the rounding mode along with the &lt;br /&gt;
scale by using .setScale(scale, roundingMode).  The rounding mode specifies&lt;br /&gt;
how to round the number.&lt;br /&gt;
&lt;br /&gt;
Why do we also want to specify the rounding mode? Let's use the BD of 1.5 from&lt;br /&gt;
above as an example,&lt;br /&gt;
&lt;br /&gt;
  bd = new BigDecimal(1.5); // is actually 1.4999....&lt;br /&gt;
  bd.setScale(1); // throws ArithmeticException&lt;br /&gt;
&lt;br /&gt;
It throws the exception because it does not know how to round 1.49999.&lt;br /&gt;
So it is a good idea to always use .setScale(scale, roundingMode).&lt;br /&gt;
&lt;br /&gt;
There are eight choices for rounding mode,&lt;br /&gt;
&lt;br /&gt;
ROUND_CEILING:  Ceiling function &lt;br /&gt;
                  0.333  -&amp;gt;   0.34 &lt;br /&gt;
                 -0.333  -&amp;gt;  -0.33&lt;br /&gt;
&lt;br /&gt;
ROUND_DOWN:  Round towards zero &lt;br /&gt;
                  0.333  -&amp;gt;   0.33 &lt;br /&gt;
                 -0.333  -&amp;gt;  -0.33&lt;br /&gt;
&lt;br /&gt;
ROUND_FLOOR:  Floor function &lt;br /&gt;
                  0.333  -&amp;gt;   0.33&lt;br /&gt;
                 -0.333  -&amp;gt;  -0.34&lt;br /&gt;
&lt;br /&gt;
ROUND_HALF_UP:  Round up if decimal &amp;gt;= .5 &lt;br /&gt;
                  0.5  -&amp;gt;  1.0&lt;br /&gt;
                  0.4  -&amp;gt;  0.0&lt;br /&gt;
&lt;br /&gt;
ROUND_HALF_DOWN:  Round up if decimal &amp;gt; .5 &lt;br /&gt;
                  0.5  -&amp;gt;  0.0&lt;br /&gt;
                  0.6  -&amp;gt;  1.0&lt;br /&gt;
&lt;br /&gt;
ROUND_HALF_EVEN:  &lt;br /&gt;
&lt;br /&gt;
Round half even will round as normal.  However, when the rounding digit is 5, &lt;br /&gt;
it will round down if the digit to the left of the 5 is even and up otherwise.&lt;br /&gt;
This is best illustrated by example,&lt;br /&gt;
&lt;br /&gt;
  a = new BigDecimal(&amp;quot;2.5&amp;quot;); // digit left of 5 is even, so round down&lt;br /&gt;
  b = new BigDecimal(&amp;quot;1.5&amp;quot;); // digit left of 5 is odd, so round up&lt;br /&gt;
  a.setScale(0, BigDecimal.ROUND_HALF_EVEN).toString() // =&amp;gt; 2&lt;br /&gt;
  b.setScale(0, BigDecimal.ROUND_HALF_EVEN).toString() // =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
The Javadoc says about ROUND_HALF_EVEN:  Note that this is the rounding mode &lt;br /&gt;
that minimizes cumulative error when applied repeatedly over a sequence of &lt;br /&gt;
calculations.&lt;br /&gt;
&lt;br /&gt;
ROUND_UNNECESSARY:&lt;br /&gt;
&lt;br /&gt;
Use ROUND_UNNECESSARY when you need to use one of the methods that requires&lt;br /&gt;
input of a rounding mode but you know the result won't need to be rounded.&lt;br /&gt;
&lt;br /&gt;
When dividing BigDecimals, be careful to specify the rounding in the .divide(...) method.&lt;br /&gt;
Otherwise, you could run into an ArithmeticException if there is no precisely rounded&lt;br /&gt;
resulting value, such as 1/3.  Thus, you should always do:&lt;br /&gt;
&lt;br /&gt;
   a = b.divide(c, decimals, rounding);&lt;br /&gt;
&lt;br /&gt;
===Immutability and Arithmetic===&lt;br /&gt;
&lt;br /&gt;
BigDecimal numbers are immutable.  What that means is that if you create a &lt;br /&gt;
new BD with value &amp;quot;2.00&amp;quot;, that object will remain &amp;quot;2.00&amp;quot; and can never be &lt;br /&gt;
changed.&lt;br /&gt;
&lt;br /&gt;
So how do we do math then?  The methods .add(), .multiply(), and so on all&lt;br /&gt;
return a new BD value containing the result. For example, when you want to &lt;br /&gt;
keep a running total of the order amount,&lt;br /&gt;
&lt;br /&gt;
  amount = amount.add( thisAmount );&lt;br /&gt;
&lt;br /&gt;
Make sure you don't do this,&lt;br /&gt;
&lt;br /&gt;
  amount.add( thisAmount );&lt;br /&gt;
&lt;br /&gt;
THIS IS THE MOST COMMON MISTAKE MADE WITH BIGDECIMALS!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Comparison===&lt;br /&gt;
&lt;br /&gt;
It is important to never use the .equals() method to compare BigDecimals.&lt;br /&gt;
That is because this equals function will compare the scale.  If the&lt;br /&gt;
scale is different, .equals() will return false, even if they are the same &lt;br /&gt;
number mathematically.&lt;br /&gt;
&lt;br /&gt;
  BigDecimal a = new BigDecimal(&amp;quot;2.00&amp;quot;);&lt;br /&gt;
  BigDecimal b = new BigDecimal(&amp;quot;2.0&amp;quot;);&lt;br /&gt;
  print(a.equals(b)); // false&lt;br /&gt;
&lt;br /&gt;
Instead, we should use the .compareTo() and .signum() methods. &lt;br /&gt;
&lt;br /&gt;
  a.compareTo(b);  // returns (-1 if a &amp;lt; b), (0 if a == b), (1 if a &amp;gt; b)&lt;br /&gt;
  a.signum(); // returns (-1 if a &amp;lt; 0), (0 if a == 0), (1 if a &amp;gt; 0)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===When to Round: Thoughts on Precision===&lt;br /&gt;
&lt;br /&gt;
Now that you can control how to round your calculations, what precision should they be&lt;br /&gt;
rounded to?  The answer depends on how you plan to use the resulting number.&lt;br /&gt;
&lt;br /&gt;
You know what the precision needed for the final result from your user requirements.  For&lt;br /&gt;
numbers which would be added or subtracted to arrive at the final result, you should add&lt;br /&gt;
one more decimal of precision, so that 0.0144 + 0.0143 will be rounded to 0.03, whereas if&lt;br /&gt;
you rounded both to 0.01, you would get 0.02&lt;br /&gt;
&lt;br /&gt;
If you need numbers which would be multiplied to arrive at the final result, you should &lt;br /&gt;
preserve as many decimal places as possible.  Ratios and unit costs, for example, should not &lt;br /&gt;
be rounded.  After the multiplication, you should round your final result.&lt;br /&gt;
&lt;br /&gt;
===Appendix===&lt;br /&gt;
====API Reference====&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html&lt;br /&gt;
&lt;br /&gt;
====OFBiz Framework====&lt;br /&gt;
&lt;br /&gt;
GenericValues have a method to get any numeric field as a BigDecimal,&lt;br /&gt;
&lt;br /&gt;
  GenericValue orderItem = delegator.findByPrimaryKey(...);&lt;br /&gt;
  BigDecimal amount = orderItem.getBigDecimal(&amp;quot;amount&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
You may also set values as BigDecimal,&lt;br /&gt;
&lt;br /&gt;
  orderItem.set(&amp;quot;amount&amp;quot;, new BigDecimal(&amp;quot;0&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
Sometimes you may want to define a service that accepts BigDecimals instead&lt;br /&gt;
of Doubles, in which case you'll have a servicedef element like this,&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;attribute name=&amp;quot;amount&amp;quot; type=&amp;quot;java.math.BigDecimal&amp;quot; ... /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you write methods in the simple-method minilang, the internal type for &lt;br /&gt;
numeric values is already BigDecimal.  However, it is a good idea to use&lt;br /&gt;
Java instead so that you may have control over the scale and rounding mode.&lt;br /&gt;
&lt;br /&gt;
Because the rounding mode and scale may be different throughout the&lt;br /&gt;
applications, it is important to set up separate properties for separate&lt;br /&gt;
usages.  There are utility methods to help you extract the properties.&lt;br /&gt;
&lt;br /&gt;
You may wish to define at the top of each Java class the following code&lt;br /&gt;
snippet,&lt;br /&gt;
&lt;br /&gt;
    // set some BigDecimal properties&lt;br /&gt;
    private static BigDecimal ZERO = new BigDecimal(&amp;quot;0&amp;quot;);&lt;br /&gt;
    private static int taxDecimals = -1;&lt;br /&gt;
    private static int taxRounding = -1;&lt;br /&gt;
    static {&lt;br /&gt;
        decimals = UtilNumber.getBigDecimalScale(&amp;quot;myconfig.properties&amp;quot;, &lt;br /&gt;
                                                 &amp;quot;tax.decimals&amp;quot;);&lt;br /&gt;
        rounding = UtilNumber.getBigDecimalRoundingMode(&amp;quot;myconfig.properties&amp;quot;,&lt;br /&gt;
                                                        &amp;quot;tax.rounding&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // set zero to the proper scale&lt;br /&gt;
        ZERO.setScale(decimals);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The getBigDecimalScale() and getBigDecimalRoundingMode() methods will return&lt;br /&gt;
the OFBiz-wide default scale and rounding mode if it is unable to read the&lt;br /&gt;
properties file.  These values are scale=2 and roundingMode=ROUND_HALF_UP.&lt;br /&gt;
If the method fails to read the configured numbers, a warning will appear in&lt;br /&gt;
the log.&lt;br /&gt;
&lt;br /&gt;
When you store back into the database via entity engine, you'll need to re-cast&lt;br /&gt;
it back into a double like this:&lt;br /&gt;
&lt;br /&gt;
   myValue.set(&amp;quot;amount&amp;quot;, new Double(amountBigDecimal.doubleValue()));&lt;br /&gt;
&lt;br /&gt;
This, however, is not really necessary: you can just force the BigDecimal object&lt;br /&gt;
into the entity engine.  Just ignore the warning message.&lt;br /&gt;
&lt;br /&gt;
====Minilang====&lt;br /&gt;
&lt;br /&gt;
Minilang supports BigDecimal in both the &amp;lt;calculate&amp;gt; and &amp;lt;set&amp;gt; operations.  When&lt;br /&gt;
calculating with minilang, you can use:&lt;br /&gt;
&lt;br /&gt;
            &amp;lt;calculate field-name=&amp;quot;postedBalance&amp;quot; type=&amp;quot;BigDecimal&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and the calculation will be performed in BigDecimal.  The default precision is 2,&lt;br /&gt;
and the default rounding mode is ROUND_HALF_EVEN.  You can also set those yourself&lt;br /&gt;
by rounding-mode=&amp;quot;&amp;quot; and decimal-scale=&amp;quot;&amp;quot; attribute tags in &amp;lt;calculate&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
When setting values, you can also use the type=&amp;quot;&amp;quot; to convert values to and from&lt;br /&gt;
BigDecimal, such as:&lt;br /&gt;
&lt;br /&gt;
            &amp;lt;set from-field=&amp;quot;postedBalance&amp;quot; field=&amp;quot;updateGlAccountParams.postedBalance&amp;quot; type=&amp;quot;Double&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which converts a BigDecimal result to a Double.&lt;/div&gt;</summary>
		<author><name>Sichen</name></author>
		
	</entry>
</feed>