New functions for rounding numbers in templates

322 views
Skip to first unread message

Chris @ oTree

unread,
Dec 26, 2020, 5:27:45 AM12/26/20
to oTree help & discussion
Hello,

In the latest release I added filters for rounding numbers in templates to 0, 1, or 2 decimal places. For example, if x = 0.12345, you can do:

{{ x|to2 }} -> 0.12
{{ x|to1 }} -> 0.1
{{ x|to0 }} -> 0

Best,
Chris

wood...@gmail.com

unread,
Jun 5, 2021, 4:55:53 PM6/5/21
to oTree help & discussion
Hi Chris,

This doesn't appear to work in the same manner as floatformat used to - in that you cannot add zeros afterwards, like changing 0.5 to 0.50, which is useful for displaying both dollars and probabilities.  Do you have any suggestions for a workaround?

Best Regards,
Daniel Woods

Chris @ oTree

unread,
Jun 7, 2021, 12:28:23 AM6/7/21
to oTree help & discussion
Good catch, this will be fixed in the next release, thanks.

Pablo Coralio Ballester

unread,
Nov 25, 2021, 9:22:50 AM11/25/21
to oTree help & discussion
Hi Chris, toX is not parametrizable, as opposed to something like "to:X", as we had with "formatfloat:X". Can you think of a workaround? My idea is that X comes from session.config...
Thanks

Chris @ oTree

unread,
Nov 25, 2021, 9:38:59 AM11/25/21
to Pablo Coralio Ballester, oTree help & discussion
You can calculate & format the number in vars_for_template. 

Sent from my phone

On Nov 25, 2021, at 6:22 AM, Pablo Coralio Ballester <coralio....@gmail.com> wrote:

Hi Chris, toX is not parametrizable, as opposed to something like "to:X", as we had with "formatfloat:X". Can you think of a workaround? My idea is that X comes from session.config...
--
You received this message because you are subscribed to a topic in the Google Groups "oTree help & discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/otree/eJPgYq_9NYc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/1a7113d9-167b-49df-ac74-d78d2517853en%40googlegroups.com.

Pablo Coralio Ballester

unread,
Nov 25, 2021, 10:08:53 AM11/25/21
to oTree help & discussion
I have lots of variables to show, so that doesn't seem to be very practical when I want to round all of them according to some parametrized rule, unless there is another way... Finally, I chose to use "to2" in all cases, which is quite OK for me. I understand that all the advances you've made with otree come at some very little cost (no more "floatformat"). Just wanted to know if there is a more "flexible" way to do this.
Thanks for your impressive work

Chris @ oTree

unread,
Nov 25, 2021, 10:31:41 AM11/25/21
to oTree help & discussion
Another option is to define the template HTML in vars_for_template, using f-strings. For example:

def vars_for_template(player):
  html = f"""
  You are player {player.id_in_group}. Your payoff is {my_float_format(player.payoff)}.
  """
  return dict(html=html)

This means you can use arbitrary Python code and call any functions you want. This approach works when you need to build some complex HTML.

Then in the template just do {{ html }}.

Chris @ oTree

unread,
Nov 25, 2021, 10:33:03 AM11/25/21
to oTree help & discussion
Just to be clear, you can also include HTML tags in there, like:

def vars_for_template(player):
  html = f"""
  <p>You are player <b>{player.id_in_group}</b>. Your payoff is {my_float_format(player.payoff)}.</p>
  """
  return dict(html=html)

Reply all
Reply to author
Forward
0 new messages