gpt4 book ai didi

javascript - 如何创建新的 MathContext?

转载 作者:行者123 更新时间:2023-12-02 19:55:16 25 4
gpt4 key购买 nike

主要问题

我正在使用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

我做错了什么? (顺便说一句,我尝试过的每个搜索引擎都返回 Java 的结果,而不是 Javascript,尽管我的第一个关键字是 javascript。)

背景

我正在尝试解决this question I asked earlier 。我确定问题在于 BigDecimal 以我不想要的方式四舍五入答案。在使用调试器跟踪代码时,我似乎需要将 MathContext 对象作为第二个参数传递给 divide() 方法。这是我的代码中的相关片段(暂时忽略魔数(Magic Number)):

// v1 and v2 are both of type BigDecimal.
v1 = v1.divide(v2, new MathContext(0, 0, false, 4));

解决我的问题的任何其他方法都是可以接受的,但我仍然想了解为什么我不能只执行 new MathContext()

最佳答案

我认为只是创建的第一个例子

context = new MathContext(...)

是正确的。至少它在我的示例中有效 here 。我直接从 github 包含 BigDecimal 库:

<script type="text/javascript" src="https://raw.github.com/dtrebbien/BigDecimal.js/master/build/BigDecimal-all-last.js"></script>

关于javascript - 如何创建新的 MathContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8752967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com