2

I'm using stored numeric values in calculations and matching situations and javascript doubles are a big "NO-NO" when doing these kind of operations.

However I can't find a solution on how to use java BigDecimal in SSJS in Xpages.

Since one should construct a BigDecimal using a string I have tried different approaches i SSJS. Whatever test the result is the same, the call is ambiguous:

Ambiguity when calling new java.math.BigDecimal(long) and new java.math.BigDecimal(int)

  1. How do I use a BigDecimal in my SSJS when values are stored in documents as Numbers?
  2. How do I use BigDecimal with a string argument when values are stored in documents as Numbers?

edit/amend: After accepting Svens answer I got a bit further and to my second question.

The value retrieved from the document is 451368 but it will be stored in variable as 451367.99999999994

How do I recover from that when the user should match against original value?

1 Answers1

4

Use Java-Objects instead:

var value = new java.lang.Integer(1);
new java.math.BigDecimal(value);
Sven Hasselbach
  • 10,455
  • 1
  • 18
  • 26
  • `var test = new java.lang.Double(document1.getItemValueDouble("PaymentAmount")); viewScope.BIG = java.math.BigDecimal.valueOf(test); ` My problem is that the value fetched is 351768 but after conversion it becomes 351767,99999994 – Mikael Andersson Wigander Mar 20 '14 at 12:52
  • 2
    The value is stored as a double in the back end, so your precision is limited by that. The main advantage of BigDecimal is to not lose any further precision when doing additional operations on the value. Have a look at the accepted answer here for more explanation: http://stackoverflow.com/questions/7856136/java-inaccuracy-using-double – Gary Forbis Mar 20 '14 at 13:01