【发布时间】:2012-02-03 21:29:00
【问题描述】:
主要问题
我正在使用this 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 的结果。)
背景
我正在尝试解决this question I asked earlier。我已经确定问题在于 BigDecimal 以我不想要的方式四舍五入答案。在使用调试器跟踪代码时,看来我需要将 MathContext 对象作为第二个参数传递给 divide() 方法。这是我的代码中相关的 sn-p(暂时忽略幻数):
// v1 and v2 are both of type BigDecimal.
v1 = v1.divide(v2, new MathContext(0, 0, false, 4));
任何其他解决我的问题的方法都是可以接受的,但我仍然想了解为什么我不能只做new MathContext()。
【问题讨论】:
标签: javascript bigdecimal