# Help with command structure - \`toFixed()\`

**URL:** https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829
**Category:** General
**Created:** [23 April 2023 01:25 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829 "2023-04-23T01:25:16Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 01:25 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/1 "2023-04-23T01:25:16Z")

</div>

I know: I'm stupid.

I did some homework and found this to resolve a _problem_.

`toFixed()`

> <https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places>

It puts a given number of decimal points in a number returned.

So.... For instance I have:

```auto
let j = 20;
let k = 3;
let answer = j/k;
node.warn("Answer is " + answer);

```

I would see `6.666666666667`

But if I did:

```auto
let j = 20;
let k = 3;
let answer = j/k.toFixed(1);
node.warn("Answer is " + answer);

```

I would get:

`6.6`

Which is what I want.

It works.

But if I do this:

` msg.payload = flow.set("rain",0).toFixed(1);`

It _spits the dummy_ at me.

Where's the elephant I'm (again) missing?

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [23 April 2023 01:49 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/2 "2023-04-23T01:49:03Z")

</div>

Flow.set(" rain",(X.toFixed(1));

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 01:51 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/3 "2023-04-23T01:51:23Z")

</div>

Thanks @gerry but from where does the `X` come?

I did say I am stupid. 😉

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [23 April 2023 01:53 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/4 "2023-04-23T01:53:19Z")

</div>

Some number, if you put a number in the statement truncate the number before. I used X to demonstrate a point

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [23 April 2023 01:55 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/5 "2023-04-23T01:55:17Z")

</div>

let X = 111.789/53.8888,  
Or whatever

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 01:56 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/6 "2023-04-23T01:56:03Z")

</div>

That's ok usually but I read WAY TOO MUCH into things sometimes and get caught up on the most stupid of things.

(Thus I am asking)

So how can I do that with the single line of code I posted with the `flow.get()` bit in it?

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [23 April 2023 02:00 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/7 "2023-04-23T02:00:49Z")

</div>

I'm having a bit of a problem. It looks like you have a zero in your statement which will always be one digit,  
The only way to get decimal numbers would be to do math in the statement. I would suggest not to do that and do all math in front of flow.set or after if a flow get

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 02:01 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/8 "2023-04-23T02:01:52Z")

</div>

So I can't _cheat_ and get `0` to show as `0.0`?

I'm not sure how to _attack_ this problem.

I am getting weather information.  
The value can be.... `0` to _anything_ with a lot of decimal places.  
So I need to _constrain_ it to `1` decimal place.

So I was messing around for a long time and was getting some days with very long values and it really messed up the display.  
Others worked fine.

I went down stupid rabbit holes then realised I need to attack this at the start.  
So I get the value and constrain it when it arrives out of the node.  
That is fine if there IS rain.  
If there isn't: I'm kinda stuck.

So I am getting an `undefined` message and catch that and convert it to `0`.

But! Ideally it should be `0.0` to comply with what I want to do with _real_ values.

To keep it from compounding itself with redundant code, it would be better if I could make `0` show as `0.0`.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [23 April 2023 02:03 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/9 "2023-04-23T02:03:08Z")

</div>

Ah, I see you question now and I don't think so unless you drop to a string

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 02:08 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/10 "2023-04-23T02:08:49Z")

</div>

Drats.

So I am going to have to have another bit of code to allow for when it is a _pure_ `0` value and force it to display as `0.0`.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 April 2023 02:09 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/11 "2023-04-23T02:09:11Z")

</div>

I assume you are doing this to display nicer values on dashboard?

The usual recommendation is that you leave numbers as they are but instead use formatters on the dashboard to display the number in the style you desire. So your question around the number zero using a formatter even the zero will display with the required decimal places.

Example instead of `payload` use `{{ payload | number : 1.2-2 }}`

#### BREAKDOWN

'1.2-2' means {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}:

A minimum of 1 digit will be shown before decimal point  
It will show at least 2 digits after decimal point  
But not more than 2 digits

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 02:15 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/12 "2023-04-23T02:15:19Z")

</div>

Thanks @Steve-Mcl.

I'll have to look at that more.

What is also a problem - maybe - is storing values.

(Sorry...)

3 ways this happens:  
1 - at time  
2 - hourly from midnight  
3 - hourly from sunrise

at time - easy enough.  
2 and 3 get the weather every hour from their times and cumulatively add the values.  
(reset at midnight/daily)

That is stored in `flow.rain` now.  
It was stored in a single node, but things got away on me and flow seemed easier.  
I don't know if that is still true or not, but for the sake of not confusing myself any more than I am just now: I'll leave that part alone.

I want to get the mechanics working.

Your tip just now may go a long way to fixing it.

Alas it is now lunch time and not having eaten a real meal in .... 2 days: I think I need to eat.  
'Puta going off just now and I shall look at it with a clearer mind after eating some food.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 04:10 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/13 "2023-04-23T04:10:28Z")

</div>

I must be more stupid than I give myself credit.

This doesn't work and yet I have done what you explained.

```auto
[{"id":"c12e7a9dc72ee1a0","type":"template","z":"d188b95f33e5f7e4","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload | number: 1.2-2}}","output":"str","x":3530,"y":1490,"wires":[["6a3fdd888a5b6a85"]]},{"id":"5ce03491fb614cba","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2.363636","payloadType":"num","x":3370,"y":1490,"wires":[["c12e7a9dc72ee1a0"]]},{"id":"6a3fdd888a5b6a85","type":"debug","z":"d188b95f33e5f7e4","name":"debug 266","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3700,"y":1490,"wires":[]}]

```

All I get in the `debug` is `""`

P.S.  
I moved the `:` from just after `number` and it made no difference. Still no output.  
P.P.S.  
I've worked out that is for use in something like a `text` node.  
I am sending my message through a `template` node to construct the _message_ from other parts (as well as the payload).

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 April 2023 04:18 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/14 "2023-04-23T04:18:38Z")

</div>

That example is angularjs. I.e. dashboard. You have used it in a regular template node (which is mustache format, not angularjs)

It's the exact format I showed in a field of a UI component (even the ui-template)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [23 April 2023 04:23 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/15 "2023-04-23T04:23:31Z")

</div>

I can understand how frustrating it is for you as you are telling me things that are true and I am not understanding them.

> [@Steve-Mcl](#):
>
> You have used it in a regular template node (which is mustache format, not angularjs)

With you to here.

> [@Steve-Mcl](#):
>
> It's the exact format I showed in a field of a UI component (even the ui-template)

Lost me.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [23 April 2023 10:44 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/16 "2023-04-23T10:44:16Z")

</div>

> [@Trying\_to\_learn](#):
>
> What is also a problem - maybe - is storing values.

Either don't worry about the value stored, if you need a float, just store a float. Correct to 1dp on display.

OR

Multiply the numbers by 10 and store as the integer version then divide by 10 on display. You will find that some IoT devices handling temperature have all of the temperatures x10 - the Wiser smart heating does this for example. It is so that they don't have to deal with floats at all except when displaying. That is typically a lot more efficient both storage wise and for embedded microprocessor systems.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 June 2023 10:44 UTC](https://discourse.nodered.org/t/help-with-command-structure-tofixed/77829/17 "2023-06-22T10:44:21Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
