I’m using this BigDecimal, and I’m trying to create a new MathContext object to pass to BigDecimal’s divide()
method. But everything I’ve tried throws exceptions saying that it’s undefined. Here are some examples of things I’ve tried that don’t work:
context = new MathContext(); // ReferenceError: MathContext is not defined
context = new BigDecimal.MathContext(); // TypeError: undefined is not a function
context = new BigDecimal.prototype.MathContext(); // TypeError: undefined is not a function
a = new BigDecimal('1'); context = new a.MathContext(); // TypeError: undefined is not a function
What am I doing wrong? (By the way, every search engine I’ve tried is returning results for Java, not Javascript, despite the fact that my first keyword is javascript
.)
I’m trying to solve this question I asked earlier. I’ve determined that the problem is that BigDecimal is rounding answers in a way I don’t want. In following the code with a debugger, it appears that I need to pass a MathContext object in as the second argument to the divide()
method. Here’s the relevant snippet from my code (ignore the magic numbers for now):
// v1 and v2 are both of type BigDecimal.
v1 = v1.divide(v2, new MathContext(0, 0, false, 4));
Any other method of solving my problem would be acceptable, but I still want to understand why I can’t just do new MathContext()
.
I think the first example of just creating
context = new MathContext(...)
is the correct one. At least its working in my example here. I'm include the BigDecimal library direct from github with:
<script type="text/javascript" src="https://raw.github.com/dtrebbien/BigDecimal.js/master/build/BigDecimal-all-last.js"></script>