主要问题
我正在使用这个BigDecimal,并试图创建一个新的MathContext对象来传递给BigDecimal的divide()方法。但我尝试过的每一件事都会抛出异常,说它是没有定义的。下面是一些我尝试过的不起作用的例子:
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我做错了什么?(顺便说一句,尽管我的第一个关键字是javascript,但我尝试过的每个搜索引擎都是返回Java的结果,而不是Javascript。)
背景
我在努力解决我刚才问的这个问题问题。我已经确定问题在于,BigDecimal正在以一种我不想要的方式舍入答案。在使用调试器的代码之后,我似乎需要将一个MathContext对象作为第二个参数传递给divide()方法。下面是我代码中的相关片段(暂时忽略神奇的数字):
// v1 and v2 are both of type BigDecimal.
v1 = v1.divide(v2, new MathContext(0, 0, false, 4));解决我的问题的任何其他方法都是可以接受的,但我仍然想了解为什么我不能只做new MathContext()。
发布于 2012-01-06 15:07:30
我认为第一个例子就是
context = new MathContext(...)才是正确的。至少它在我的示例这里中起作用。我包括直接来自github的BigDecimal库:
<script type="text/javascript" src="https://raw.github.com/dtrebbien/BigDecimal.js/master/build/BigDecimal-all-last.js"></script>https://stackoverflow.com/questions/8752967
复制相似问题