2011-11-21 36 views
0

我有通過地圖迭代,並顯示該信息爲這樣的模板:爲什麼模板不能提供正確的值?


     #{list items:report.getCategoryMap()?.keySet(), as:'cat'} 
      %{models.reporting.TransactionReportItem item = report.getCategoryMap()?.get(cat);}% 
      
       ${cat} 
       ${item?.nbCredit} 
       ${item?.getCreditPerc(report.nbCredit)} 
       ${item?.nbDebit} 
       ${item?.getDebitPerc(report.nbDebit)} 
       ${item?.getTotalTransactions()} 
      
     #{/list} 

出於某種原因,模板總是呈現getCreditPerc和getDebitPerc的結果爲0.0


    public Double getCreditPerc(long totalCredit){ 
     double perc = (double) (nbCredit/totalCredit); 
     Logger.info("nbCredit: %s, total cr: %s", nbCredit, totalCredit); 
     return new Double(perc); 
    } 

當調用模板我可以看到日誌中的輸出:

2011-11-21 13:54:22 INFO〜[TransactionReportItem:85] getDebitPerc() - nbDebit:39,total cr:4984

我嘗試使用原始類型而不是雙重對象沒有成功。 當調試代碼時,我可以看到所有的值都被正確設置。

難道這與groovy模板渲染有關嗎?

回答

4

發佈從不同功能的記錄並沒有幫助這個問題一起;-)

然而,問題是,你在Java中有兩個ints,而你將他們讓你得到整數除法.. 。

鑄造這個整數,然後到double爲時已晚......

嘗試:

double perc = (double)nbCredit/totalCredit ; 
+0

沒錯剛剛意識到。 TX。 – emt14

+0

沒有看到您的帖子,我張貼相同;) – mandubian

1

愚蠢的ID來自我的電子郵件。這不就是代碼中的問題嗎?

int nbCredit=39; 
int total=4984; 
double perc = (double) (nbCredit/totalCredit); // gives 0 

nbCredit /總爲整數的使用結果< 1的分工所以它可能被四捨五入到0

做你嘗試用:

double perc = (double)nbCredit/totalCredit; 
+0

是的。我意識到發佈後...... – emt14

相關問題