Help with command structure - `toFixed()`

I know: I'm stupid.

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

toFixed()

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

So.... For instance I have:

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

I would see 6.666666666667

But if I did:

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?

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

Thanks @gerry but from where does the X come?

I did say I am stupid. :wink:

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

1 Like

let X = 111.789/53.8888,
Or whatever

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?

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

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.

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

1 Like

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.

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

1 Like

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.

I must be more stupid than I give myself credit.

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

[{"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).

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)

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

With you to here.

Lost me.

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.

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