2012-01-06 65 views
0

主要問題

我正在使用this BigDecimal,我試圖創建一個新的MathContext對象來傳遞給BigDecimal的divide()方法。但是我試過的一切都會拋出異常,說它沒有定義。這裏有一些事情的例子我已經試過了工作:如何創建新的MathContext?

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()方法。下面是從我的代碼中的相關片段(忽略幻數現在):

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

解決我的問題,是可以接受的任何其他方法,但我還是想知道爲什麼我不能只是做new MathContext()

回答

2

我覺得只是創造

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> 
+0

事實證明,你使用的是我以後的版本,而且你的版本包含一個相關的bug修復。更新我的版本解決了我的問題。 – 2012-01-06 17:08:11