Formatting number to 2 digits; just cosmetics ;-)

Hi,

I use the ‘Wasmachine’ flow to be noticed when the washingmachine has finished.
As an addition i also calcultate the costs of the power used.
With mathjs i round this calculation to 2 digits behind the separator.

Sometimes the calculation gives 20 cents. This is noted as € 0,2 in the message i send to myself.
Just for cosmetic reasons: Is there a way to format this into € 0,20 (2 digits so with the 0 added) in mathjs ? (number to string)
Like something as: usedpower$ = format$(number, € ##,00)

Peter

If Homey supports all of MathJS, you should be able to use this:

format(VALUE, { precision: 2 })

This will also perform the rounding operation.

@robertklep

Thanks for the quick repsonse; unfortunatly it does not work; Homey does not recognize ‘format’.

Yhx
Peter

Maybe round will work:
From @Canedje:
something like this wil work:
{{round (Energiekosten vandaag,2)}}

the 2 means 2 digits after the ,

in your case {{round(Target temperature,1)}}

He already used round(), but that function will drop any trailing zero’s.

Yep, using it already.

Round does not ‘format’ the number.

Peter

Maybe adding 0.000001 to the outcome, than Round and extract 0.000001. Maybe you have to round it again.

Hi all.

About the format() funtion. It looks like it isn’t supported?
I’m no coder, so be warned :wink:
Maybe Robert knows what I’m doing wrong here?

I want a number to be formatted as n.0 (always display the zero) and found this at math.js:

math.format(2.3,  {notation: 'fixed', precision: 4})  // returns '2.3000' 

(In case of notation ‘fixed’, precision defines the number of significant digits after the decimal point)
So, with this Logics card ‘calc a value’, I want to accomplish 100 to be shown as 100.0 like this, but I get NaN values returned.

  • I tried this:
{{format(100, { notation: 'fixed', precision: 1 })}}
{{format(100, { 'fixed', precision: 1 })}}
{{format(100, { fixed, precision: 1 })}}
{{format(100, { precision: 1 })}}
{{format(100, { 1 })}}

Something else:
This should round to 1000, but it also returns NaN:

{{format(1234, 2)}}

Background of what I’d like to accomplish:

  • Vars used:
    • <W3_Luchtdruk/10> - a Logics NumericVar
    • <Druk> - a value between 0 and 2000 form a airpressure sensor
      VirtualDevice type Thermostat - should display 100.5 or 100.0 as airpressure

Calculate <W3_Luchtdruk/10>
as

{{format(<Druk>/10, { notation: 'fixed', precision: 1 })}}

Flow:

Not sure what you’re doing wrong, that works just fine for me:

and the result:

What also works is this:

{{ format(1234, { precision : 1, notation: "fixed" }) }}

If you use single quotes (or back quotes) it stops working. I guess Athom still hasn’t worked out the parsing issues in flow cards.

Thanks, Robert. Glad to know the function itself works.

I can’t see the difference either.
I will continue trying, maybe creating a new flow might help.
Cheers

This worked for me as well, Robert, using the math directly in the push message

I found out it doesn’t work with the native logic “calculate a value” card, the result is an empty num var:

But, it works a little bit with better logic “Execute matjs expression” card, but the result stays zero

Again, in the push message it works fine.

Now I have to find out how to get the calculated value into the virtual thermometer…

Perhaps try variable names without mathematical operators (-) in their names.

Thanks Robert. I realized this and changed the var name a few times.
First I used a / sign and before that a . sign. Will now try without special characters.

Is there a way to get the output to exclude the " "
I am wanting to turn a number (23) into 23.0 but the results outputs as “23.0” and the web command then fails.

It seems that the flow cards are adding these (possibly because they are using some sort of JSON formatting). I don’t know if there’s a way of removing them.