What the usage of the format function


#1

I need to put the temperature with only 1 decimal. My device is displaying 2 decimal by default.

Please help me to understand the format function and it parameters.

Thank in advance.


#2

I have this in one of my tiles.

format(’%.1f’,decimal([Tado Heating : temperature]))


#3

The webCoRE wiki has help for all supported functions - including the format function. That section includes two references with more details.

To answer your question, in your case you would use

format('%0.1f', [MyDevice:temperature])

#4

thank you !


#5

Thanks for this. I used this extensively yesterday, and then again today.

Getting an error with my latest attempt.

This is what I’m entering in the text (expression)
format( ‘%.1f’, [Outside Temperature:temperature])

and it outputs to this:
java.util.illegalFormatConversionException: f!=java.util.ArrayList


#6

Do you have curlies in there at all?

{format('%.1f', [Outside Temperature:temperature])}

Also, if your value is actually a string you’ll need to translate it as well. I think that may actually be the issue you have.


#7

…and since there is more than one way to skin a cat, you can also round numbers to a specific number of decimals by doing something like this in your expression box:

round(variableName,1)

(the example above rounds to one decimal place)

I think, for your example, you could use:

round([MyDevice:temperature],1)


#8

No curlies. That code above it’s a copy and paste from the Piston…

Moreover, the format command does not work at times, especially when the data source is a full number.

And the reason I don’t want to use the “round” command is because I always want it to show to one decimal place, which “round” does not achieve. And now I’ve noticed the format command does not either. It’s this a bug?


#9

I have noticed the “.0” being dropped as well. Personally, I have always liked that, but I can see how certain situations, we’d want to force the “dot zero” to be shown.

I vaguely remember reading about a workaround of multiplying the number by 10, then rounding to a whole number, then dividing by 10 to force the dot zero to stick… but I don’t remember any more specifics than that.

Maybe someone else can chime in


#10

Paging @Robin. Is this a bug, or infact, 2 bugs?


#11

Well, in all fairness, some would call displaying 56.0 as 56 a feature, not a bug… but I agree with you there should be a way to force the decimal (if desired).