Round template (sensor) to n decimal places, including ending 0's

Hi all,

I found countless posts on how to round to 1/2/3/n decimal places with different solutions.

round(n) is by far the most suggested/utilized by the member of this community, including me. Unfortunately this solution technically works fine but I’d like to see 0’s instead of blank space in the lovelace.

{{ 1.234|round(2) }}
{{ 1.230|round(2) }}
{{ 1.200|round(2) }}
{{ 1.000|round(2) }}

Actual Result

1.23
1.23
1.2
1.0

Wanted Result when seen in Lovelace:

1.23
1.23
1.20
1.00

Is anyone aware of a solution to this?
This only bothers me because when I pull one of these sensors out in the lovelace I see something like:

1.58 £
1.59 £
1.6 £
1.61 £
1.62 £

And this triggers me quite a lot.
I tried adding device_class: monetary to the sensor but doesn’t help.

Anyone has a workaround?

The only workaround is to prepend/append one or more characters to the number so that it is forced to be interpreted as a string value.

For more information, refer to this post:

1 Like

I was testing with a different approach and I saw that this was working for a few people.

{{ '%.2f' | format(1.6) }} £

Returning
1.60 £

Surprisingly, it only works if you include the “ £” at the end, otherwise, without, won’t work anymore…

{{ '%.2f' | format(1.6) }}

Returning
1.6

I’m even more confused…

Looks like we’ve got to wait for the lovelace to be able to understand that a given value is monetary (device_class: monetary) and visualize it in the correct way.

It’s because of how the “native types” feature works (explained in the linked post). If the template’s output looks like a floating point number, it is handled as a floating point number. So the string ‘1.2000’ becomes 1.2 because there’s no need to represent a floating point number with trailing zeros.

However, if you include a non-numeric character in the string then the “native types” feature cannot interpret it as an integer or floating point number so it’s handled as a string.

1 Like

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been solved (the workaround was explained). It will help users find answers to similar questions. For more information refer to guideline 21 in the FAQ.