# Suffix for text output

**URL:** https://discourse.nodered.org/t/suffix-for-text-output/89879
**Category:** Dashboard
**Tags:** dashboard-2
**Created:** [30 July 2024 12:36 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879 "2024-07-30T12:36:44Z")
**Posts on this page:** 11
**Page:** 2

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 July 2024 14:23 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/21 "2024-07-30T14:23:38Z")

</div>

Is it naive of me to think that one might allow the use of a JSONata specification inside the ui-text node as a method of doing all of this without additional nodes?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [30 July 2024 14:26 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/22 "2024-07-30T14:26:05Z")

</div>

But just don't forget about the performance of the JSONata ...,

---

<div class="post-metadata">

### Author: ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)
#### Post date: [30 July 2024 14:36 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/23 "2024-07-30T14:36:51Z")

</div>

Quite possibly - although I really don't like the readability of JSONata - I find it very unintuitive.

Point acknowledged on the `toFixed` though - any thoughts @Steve-Mcl?

Still trying to think how we could _safely_ re-introduce the value format field, and without hacks left-right and center as in D1.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: [30 July 2024 15:05 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/24 "2024-07-30T15:05:22Z")

</div>

> [@joepavitt](#):
>
> Quite possibly - although I really don't like the readability of JSONata - I find it very unintuitive.

100%! A man after my own heart!

> [@joepavitt](#):
>
> Point acknowledged on the `toFixed` though - any thoughts @Steve-Mcl?

> [@Colin](#):
>
> Unfortunately if you want to show `{{payload.toFixed(2)}} °C`, which was also possible in the D1 nodes,

This is not something that Mustache supports - but (IIRC) a ~~hack~~ workaround. In fact, I am pretty sure @dceejay has mentioned this recently.

One solution would be the use of filters (like the angular filters). There is such a concept for vue ([example](https://talkingtech.io/filters-in-vue-js/)) but I remember reading recently they were dropped from Vue 3.

Vue [has written](https://v3-migration.vuejs.org/breaking-changes/filters.html) the recommended approach now is to use `computed` however this does not help in cases like this where you are not coding but there is a 2nd alternative suggestion `"globalProperties"` in that link that _might_ be useful (assuming the D1 mustache-ish syntax support is added to D2).

In short, it would mean we may be able to offer support for things like `{{ $filters.formatNumber(payload, { fixed: 2, suffix: ' °C' }) }}`

The thing is, it isn't really low-code nor is it JavaScript. Its not even JSONata, its another awkward workaround. Don't get me wrong I want a better way of formatting values but I am not certain the original D1 way was the best and so mimicking it in d2 may be worse current solution.

* * *

### outlandish ideas:

# 1

How about a dashboard config section that accepts JS code where a user can add to a list of "filters". This set of filters would then be presented on the node and the user selects the appropriate filter for the data. Of course there would be a default set like "2 decimal places", "uppercase", "title case" etc but users could then generate their own "divide by 100 and suffix with ' volts'" filter.

# 2

How about (forgive me) permitting the option of `msg.` and `JSONata` for the fields that require formatting? Easier to implement BUT harder for users (and a chain around the necks of D2 devs for ever more)

# 3

Something else?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 July 2024 15:14 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/25 "2024-07-30T15:14:50Z")

</div>

> [@hotNipi](#):
>
> But just don't forget about the performance of the JSONata ...,

I am not sure there is much difference between it and JS nowadays. I have just tested running 1,000,000 times a function node containing

```auto
msg.payload = `${msg.payload.toFixed(2)} C`
return msg

```

and a Change node containing Set msg.payload to JSONata  
`$formatNumber(payload, "#0.00;(#0.00)") & " C"`  
and they both took 22 seconds on an i5 processor. That is 22 microseconds each, which is trivial. In fact I find that difficult to believe so if someone would like to check that would be interesting. I won't publish the flow as there is no point using that in case there is an error there.

> [@joepavitt](#):
>
> although I really don't like the readability of JSONata - I find it very unintuitive.

Same here, but for simple stuff like this it is not that bad, and the current alternative of JS also gives beginners problems.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 July 2024 15:16 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/26 "2024-07-30T15:16:00Z")

</div>

Not really any performance hit on formatnumber() as it is not mapping a large array.

 ![chrome_screenshot_30 Jul 2024 16_14_35 BST](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9ee60fc3869de53adec15ffdfe45abda5204afb4.png)

> [@joepavitt](#):
>
> Quite possibly - although I really don't like the readability of JSONata - I find it very unintuitive.

I feel this is not a good reason to exclude an idea, your personnel preference sould be the last consideration.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [30 July 2024 15:41 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/27 "2024-07-30T15:41:59Z")

</div>

> [@Steve-Mcl](#):
>
> This set of filters would then be presented on the node and the user selects the appropriate filter for the data. Of course there would be a default set like "2 decimal places", "uppercase", "title case" etc

Sometimes it's necessary to pass the payload fully formatted, for example the same field might sometimes show `4.9 mi/kWh`, `49 MPG` or `5.76 L/100km`

Maybe we need a new node type which accepts payload & format (sprintf syntax springs to mind) and emits the formatted string?

Then no need to handle any of this in the dashboard widgets, and Jsonata speakers can use the change node.

---

<div class="post-metadata">

### Author: ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)
#### Post date: [31 July 2024 05:45 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/28 "2024-07-31T05:45:36Z")

</div>

> [@E1cid](#):
>
> I feel this is not a good reason to exclude an idea, your personnel preference sould be the last consideration.

You're absolutely right, though my job is to make Dashboard easy to use and as intuitive as possible.

What everyone is asking for here _should_ be easy, I'm just not sure that JSONata is that answer as it doesn’t cover things like `toFixed`

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [31 July 2024 06:50 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/29 "2024-07-31T06:50:43Z")

</div>

> [@joepavitt](#):
>
> I'm just not sure that JSONata is that answer as it doesn’t cover things like `toFixed`

I refer you to

> [@Colin](#):
>
> ```auto
> msg.payload = `${msg.payload.toFixed(2)} C`
> return msg
> 
> ```
> 
> and a Change node containing Set msg.payload to JSONata  
> `$formatNumber(payload, "#0.00;(#0.00)") & " C"`  
> and they both took 22 seconds on an i5 processor.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [31 July 2024 07:49 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/30 "2024-07-31T07:49:58Z")

</div>

> [@Colin](#):
>
> and they both took 22 seconds on an i5 processor.

Just to note that this was for 1,000,000 operations.

---

<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: [30 August 2024 07:50 UTC](https://discourse.nodered.org/t/suffix-for-text-output/89879/31 "2024-08-30T07:50:28Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/suffix-for-text-output/89879.md?page=1)
