Showing currency, with two decimals always

Hi all!

I’ve built a sensor that calculates costs for energy consumption. It rounds to two decimals. But if there’s only one decimal, it shows one decimal. Normally that’s fine. Yet, as the sensor calculates an amount of money in euro’s, I actually want the sensor to always show two decimals.

Here’s the code for template sensor:

value_template: "{{ (float(states.input_number.power_consumption_total_costs_today.state)) | round(2) }}"

What should I do?

Try this and let me know if it works

value_template: "{{ "{:,.2f}".format(states('input_number.power_consumption_total_costs_today')) }}"
1 Like

It works, thanks @lolouk44 !

I did have to change the second set of double quotes to single quotes. And I had to make it a float. So I ended up with this code:

value_template: "{{ '{:,.2f}'.format(float(states('input_number.power_consumption_total_costs_today'))) }}"

1 Like

Sorry to revive an old thread but I’m looking to format a number to currency the European way. That means a dot as thousand seperator and a comma for decimals. Then this also be done using format()?

Be warned, using the above template or the template below will break history graphs. Anyways, here’s what it would look like:

{% set v = '{:,.2f}'.format(states('input_number.power_consumption_total_costs_today') | float) %}
{{ v.replace(',','x').replace('.',',').replace('x','.') }}
1 Like