Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format a number/date in play 2.0 template?

I'm struggling a bit with the play 2.0 templating system; I have a method that returns what in scala is an 'Int' and I want to format it using the pattern "#,###".

I have tried @order.itemCount().format("#,###") and @( order.item.count() format "#,###" ) but no love.

I'm not sure if there's a trick to this in the play template syntax or whether I just need to learn how to do this in Scala.

like image 759
akbertram Avatar asked May 09 '12 11:05

akbertram


1 Answers

The most straightforward approach would be to use Java's string formatting:

scala> val n = 1000000
n: Int = 1000000

scala> "%,d".format(n)
res0: String = 1,000,000

This will also handle localization correctly:

scala> String.format(java.util.Locale.ITALY, "%,d", n: java.lang.Integer)
res1: java.lang.String = 1.000.000
like image 146
Travis Brown Avatar answered Nov 12 '22 06:11

Travis Brown