在Play 2模板中格式化double的正确方法是什么

Sim*_*mon 2 scala playframework-2.0

这是我在Play 2模板中的缩写代码......

@(variable: com.mypackage.Variable) 

<div class='statsbody'>
    <div class='statsform'>
        <label>Average:</label>
        <span>@"%.2f".format(variable.getAverage())</span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我收到编译错误:

`identifier' expected but `"' found
Run Code Online (Sandbox Code Playgroud)

我从这个问题中得到了上面的想法,该问题说明如何在scala命令提示符下执行此操作,这很好,但在模板中不起作用.

getAverage()方法属于我正在使用的外部Java包,它返回一个raw double.这一切都很好,没有格式化我可以愉快地显示正确的数字.

我尝试了各种替代方法,包括使用静态Java String格式化方法......

@String.format("%.2f", variable.getAverage())
Run Code Online (Sandbox Code Playgroud)

......给了...

Overloaded method value [format] cannot be applied to (String, Double)
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,在Play 2模板中格式化双精度的正确方法是什么?我知道我可以使用Javascript,但我想知道是否有Play/Scala解决方案.

Inf*_*ity 11

使用括号:

@("%.2f".format(variable.getAverage()))
Run Code Online (Sandbox Code Playgroud)

要么:

@{
  "%.2f".format(variable.getAverage())
  //more scala code
}
Run Code Online (Sandbox Code Playgroud)

它允许您在模板中编写多语句scala代码.