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.

This is a follow-up to the thread Formatting number to 2 digits; just cosmetics :wink:, I have a similar requirement I would like to implement. I have a number variable with multiple digits that I want to assign to a string variable with control of the format. In Perl (yes, old guy here…), this would look like this:

my $fEnergyDiff=1.23456789;
my $strEnergyDiff=sprintf(“%.2f”,$fEnergyDiff);
printf “diff=$strEnergyDiff\n”;

This would produce the string “diff=1.23”. If I can do this in a Homey flow, I can use the string variable (strEnergyDiff) everywhere I need it, for example in log entries or in push messages.

Here is the test flow I have for this:

When I run this, I get this in the Timeline: “fEnergyDiff=1.23456789, strEnergyDiff=1.23456789" (from step #3), as expected.

So, is it possible to do some clever JS stuff in step #2 so that the string variable will be assigned 1.23 and I get “fEnergyDiff=1.23456789, strEnergyDiff=1.23" in the Timeline? I did try some of the suggestions in the linked thread, but got nowhere.

Have you tried using:

const stringObj = numObj.toFixed(2)

Well, I tried, but I am pretty sure I did not do this correctly:

This produced the following in the Timeline: “fEnergyDiff=1.23456789, strEnergyDiff={{1.23456789.toFixed(2)}}”.

What is the syntax for using the toFixed function on the fEnergyDiff object and assign it to strEnergyDiff?

I think you will need to use the calculate card to round the number and then convert that to text.

1 Like

Thanks, this seems to work fine:

When I run this, card #2 does the rounding and card #4 produces the correct 2-decimal numbers in the Timeline. Since Num already has 2-decimals (due to the round function), I don’t really need the Str variable (card #3).

I found during this testing that card #1 doesn’t work, the setting of this variable is ignored, the calculation in card #2 uses the value I set in the web interface instead (from the Variables button in the flow editor), card #1 is ignored. I am not sure why this happens - maybe somebody can explain it.

Anyway, thanks Adrian - your tip did the trick! /Erlend

1 Like