# Formatting number to 2 digits; just cosmetics ;-)

**URL:** <https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734>\
**Category:** Flows\
**Created:** [October 30, 2020, 11:09am UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734 "2020-10-30T11:09:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvkan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pvkan/32/16433_2.png) [@pvkan](https://community.homey.app/u/pvkan)\
**Post date:** [October 30, 2020, 11:09am UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/1 "2020-10-30T11:09:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [October 30, 2020, 12:10pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/2 "2020-10-30T12:10:44Z")

</div>

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

```auto
format(VALUE, { precision: 2 })

```

This will also perform the rounding operation.

---

<div class="post-metadata">

**Author:** ![pvkan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pvkan/32/16433_2.png) [@pvkan](https://community.homey.app/u/pvkan)\
**Post date:** [October 30, 2020, 12:42pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/3 "2020-10-30T12:42:18Z")

</div>

@robertklep

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

Yhx  
Peter

---

<div class="post-metadata">

**Author:** ![Wessel](https://avatars.discourse-cdn.com/v4/letter/w/53a042/32.png) [@Wessel](https://community.homey.app/u/Wessel)\
**Post date:** [October 30, 2020, 1:37pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/4 "2020-10-30T13:37:22Z")

</div>

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)}}

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [October 30, 2020, 1:45pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/5 "2020-10-30T13:45:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pvkan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pvkan/32/16433_2.png) [@pvkan](https://community.homey.app/u/pvkan)\
**Post date:** [October 30, 2020, 7:32pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/6 "2020-10-30T19:32:38Z")

</div>

Yep, using it already.

Round does not ‘format’ the number.

Peter

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [October 31, 2020, 9:59pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/7 "2020-10-31T21:59:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 19, 2021, 3:10pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/8 "2021-04-19T15:10:06Z")

</div>

Hi all.

About the `format()` funtion. It _looks like_ it isn’t supported?  
I’m no coder, so be warned 😉  
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](https://mathjs.org/docs/reference/functions/format.html):

```auto
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:

```auto
{{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:

```auto
{{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

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

```

Flow:

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 19, 2021, 5:50pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/9 "2021-04-19T17:50:45Z")

</div>

> [@Peter\_Kawa](#):
>
> This should round to 1000, but it also returns NaN:
> 
> ```auto
> {{format(1234, 2)}}
> 
> ```

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/6/b60a1a3d5aebb71baf555b967cb4a62acfa6c022.png)

and the result:

 ![IMG_9FC06C58EF6D-1](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/f/2f8893c698e2993e04a28d51a0b4199c45c027bf.jpeg)

What also works is this:

```auto
{{ 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.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 19, 2021, 6:47pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/10 "2021-04-19T18:47:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 19, 2021, 7:26pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/11 "2021-04-19T19:26:00Z")

</div>

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

 ![Screenshot_20210419-205747_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/e/2eee3d4fc63076589e1744d6546fe4b408f249c5.jpeg)

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

 ![Screenshot_20210419-211854_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/1/e1c9f977e223486e1112d90c42020ca8a71a7367.jpeg)

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

 ![Screenshot_20210419-211629_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/6/d6ffff11d9f48acee83eeeb35a63f9f52fcec39d.jpeg)

Again, in the push message it works fine.

 ![Screenshot_20210419-214503_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/f/8f35dd02fffb7e3e83d3fdfabc356fe6aa79e37b.jpeg)

 ![Screenshot_20210419-213830_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/8/88d63ddd6abb0219544758a7cc0411b858df4d16.jpeg)

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [April 20, 2021, 5:33am UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/12 "2021-04-20T05:33:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 20, 2021, 12:26pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/13 "2021-04-20T12:26:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nigel\_Scott](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/nigel_scott/32/10602_2.png) [@Nigel\_Scott](https://community.homey.app/u/Nigel_Scott)\
**Post date:** [January 24, 2023, 11:02am UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/14 "2023-01-24T11:02:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 24, 2023, 12:11pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/15 "2023-01-24T12:11:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Erlend63](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erlend63/32/38414_2.png) [@Erlend63](https://community.homey.app/u/Erlend63)\
**Post date:** [December 25, 2025, 3:13pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/16 "2025-12-25T15:13:51Z")

</div>

This is a follow-up to the thread [Formatting number to 2 digits; just cosmetics 😉](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734), 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:

 ![2025-12-25 16_08_16-_new 3 - Notepad++](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/f/9f33953746219c736db7e79625ebae8bdcc03f2d.png)

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.

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [December 25, 2025, 4:22pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/17 "2025-12-25T16:22:13Z")

</div>

Have you tried using:

> const stringObj = numObj.toFixed(2)

---

<div class="post-metadata">

**Author:** ![Erlend63](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erlend63/32/38414_2.png) [@Erlend63](https://community.homey.app/u/Erlend63)\
**Post date:** [December 25, 2025, 5:29pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/18 "2025-12-25T17:29:43Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/6/164b3feaa142b482bf0fdaf05632fc08885f89b7.png)

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?

---

<div class="post-metadata">

**Author:** ![Adrian\_Rockall](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adrian_rockall/32/13209_2.png) [@Adrian\_Rockall](https://community.homey.app/u/Adrian_Rockall)\
**Post date:** [December 25, 2025, 6:29pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/19 "2025-12-25T18:29:11Z")

</div>

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

 ![Screenshot_20251225_182832_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/6/a6c0ef8939311db4a17a17a020a9d22f762c7f53.jpeg)

---

<div class="post-metadata">

**Author:** ![Erlend63](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/erlend63/32/38414_2.png) [@Erlend63](https://community.homey.app/u/Erlend63)\
**Post date:** [December 25, 2025, 9:58pm UTC](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734/20 "2025-12-25T21:58:50Z")

</div>

Thanks, this seems to work fine:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/d/9d14e051d7825fe57f4207e2ebf98e411e1f173f.png)

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

[Next page](https://community.homey.app/t/formatting-number-to-2-digits-just-cosmetics/36734.md?page=2)
