I am writing test cases with the JavaScript BigDecimal library by Daniel Trebbien (specifically the BigDecimal-all-last.min.js) and I can't seem to use the MathContext object in the divide method without throwing the error "MathContext() Bad form value"
Since this library is a direct port from Java, I am using this documentation. Since both the "form" (parameter #2) and "rounding mode" (parameter #4) are just constants, I don't understand why I am getting a "bad form value" error. My Webstorm IDE sees the constants just fine so I know they are there. Changing "form" constants to "ENGINEERING" or any of the others does not fix the issue.
var mc = new MathContext(3, BigDecimal.PLAIN, false, BigDecimal.ROUND_HALF_EVEN);
var two = new BigDecimal("2");
var seven = new BigDecimal("7");
var twoSevenths = two.divide(seven, mc); // constructor divide(BigDecimal, MathContext)
console.log(twoSevenths.toString()); // expecting 0.286
Do you see anything I am doing wrong?
**
There ARE other divide constructors one could use of course, but the multiple re-use of a single MathContext object would be ideal for a Tiny JavaScript project and thus is the focus here.
**
There is a small typo in your code. The PLAIN
constant is actually defined in MathContext
, so the first line should be:
var mc = new MathContext(3, MathContext.PLAIN, false, BigDecimal.ROUND_HALF_EVEN);
With this fix, "0.286" is logged to the console as expected.