Decimals, they are everywhere. From the price of an item at $9.98 to numbers like π (3.14159). How can these numbers be used in IBM BPM? There is a base variable (business object) type of Decimal which can hold floating point numbers. Storing these numbers in business objects works just fine until mathematical operations (+, , * /) are performed. Why is that? This is due to converting base 10 digits to base 2 digits; the basic info out of the ⇒JavaScript operations have limitations with numerical precision.
A quick overview of numbers
Z  Set of all negative and positive integers (…2,1,0,1,2…)
Q  Rational numbers – set of all numbers which can be expressed as a/b where a, b∈ Z and b ≠ 0.
P  Irrational numbers, like π (3.14159...), e (2.71828...) , and √2 (1.4142...) which cannot be expressed as a rational number
R  Real numbers, union of irrational and rational numbers: P ∪ Q
The set of integers (Z) can be expressed in base 2. Each place is a power of 2.
bit place  8  7  6  5  4  3  2  1 
power of 2  2^7  2^6  2^5  2^4  2^3  2^2  2^1  2^0 
value  128  64  32  16  8  4  2 
1

Decimal and binary representation of some integers
Decimal
 Binary 
4  0100 
17  010001 
64  01000000 
91  01011011 
Integers (Z) can easily be expressed as binary digits. However, not all fractions (rational numbers) can be represented in binary. The reason is converting these to binary results in an repeating number. ⇒See this article for information on decimals and binary, and here on ⇒StackOverFlow. This is also a study of numerical analysis.
How do you solve this problem?
Use a 3rd party math library. For my example I used the ⇒BigDecimal.js library. To make the BigDecimal.js library to work with BPM and our class loader, use the uncompressed JS files and put the MathContext.js contents pasted before the BigDecimal.js file in a single file. Then add this file as a managed asset to the application. From here you can then create statements like this.
var a = new BigDecimal(tw.local.n1.toString());
var b = new BigDecimal(tw.local.n2.toString());
a = a.add(b);
tw.local.n3 = a.toString();
Then you can get the values you want. For multiplying decimal values, these can easily add extra digits. For this you can add statement like
a = a.setScale(tw.local.customScale,
BigDecimal.prototype.ROUND_HALF_UP);
Where tw.local.customScale is an integer with the default value of 2. To enable the rounding, click the round in the BigDecimal example or the floor checkbox on the decimal version. For the plain JS Decimal version, you can use the technique of multiply by 100 and then floor, then divide by 100. Here I have an environment variable holding the value 100, so it could be changed to 1, 10, 1000 and so on.
var d1 = tw.local.n1;
var d2 = tw.local.n2;
var d3 = d1+d2;
tw.local.n3 = Math.floor(d3*tw.env.precisionDecimal) / tw.env.precisionDecimal;
An additional library for math functions is Math.js ⇒http://mathjs.org. Download section for math.js ⇒https://cdnjs.com/libraries/mathjs. The v3.0 math.js libraries require JavaScript 1.7 R4 which is available in BPM 8.5.7.0 and later. For earlier versions of BPM, download a 2.2 or earlier version.
The Example TWX file is available in the BPM L2 Sample Apps section.
Change History: 1/19/2018  Updated links to GIT repository
Disclaimer:
THE DOWNLOAD(S) ARE PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER PROPRIETARY RIGHTS OF OTHERS. NEITHER THE AUTHOR NOR IBM WARRANT THAT THE FUNCTIONS, ROUTINES, AND DATA CONTAINED IN, OR GENERATED AS A RESULT OF THE DOWNLOAD(S), WILL MEET YOUR REQUIREMENTS OR BE ERRORFREE. The entire risk related to the quality and performance of the download(s) and sample(s) is with you. In the event that there is any defect, you assume the entire cost of all necessary services, repair or correction. IN NO EVENT WILL THE AUTHOR OR IBM BE LIABLE TO YOU OR TO ANY THIRD PARTY FOR ANY DIRECT OR INDIRECT DAMAGES (INCLUDING, WITHOUT LIMITATION, LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES) ARISING OUT OF THE USE OR INABILITY TO USE THE DOWNLOAD(S), EVEN IF THE AUTHOR OR IBM HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Some jurisdictions do not allow the exclusion of implied warranties or the limitation or exclusion of liability for incidental or consequential damages, so some of the above may not apply to you."
Tags:
manager
business_process
point
process
business
ibmblog
numbers
bpm
floating