【问题标题】:Why isn't the template rendering the correct values?为什么模板没有呈现正确的值?
【发布时间】:2011-11-21 10:59:02
【问题描述】:

我有一个模板,它遍历地图并显示信息:


       #{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 模板渲染有关?

【问题讨论】:

    标签: java groovy playframework template-engine


    【解决方案1】:

    从不同的功能发布日志对这个问题没有帮助;-)

    但是,问题是您在 Java 中有两个 ints,并且您将它们除以整数除法...

    然后将此整数转换为 double 为时已晚...

    试试:

    double perc = (double)nbCredit / totalCredit ;
    

    【讨论】:

    • 没有看到你的帖子,我也发了同样的帖子;)
    【解决方案2】:

    我的一个愚蠢的想法。不就是代码的问题吗?

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

    nbCredit/total 是整数的除法,结果

    你试过了吗:

    double perc =  (double)nbCredit / totalCredit;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 2014-08-12
      • 1970-01-01
      • 2020-11-27
      • 2022-06-13
      • 2021-10-20
      • 2011-09-01
      • 2015-04-01
      相关资源
      最近更新 更多