Number formatting

I displaying some numbers from a pageData payload originating from a table.
These numbers currently have 16 decimals, so is there a way to format that down to 3 decimals using handlebars?

Hi @Lars_Andersson,

The {{format}} helper should do the trick here!

Thanks,
Julia

I tried {{format mynumber}} and {{format mynumber ‘,.6’}} but that didn’t change anything.

I think I figured that one out. the “mynumber” variable was a string.

Can I convert string to number with {{format}} too?
I have tried to find that before asking, but no luck.

@Lars_Andersson,

I recommend using the String Node. :hugs:

Yeah, I was looking at that, but since I am calling rows from a table and not all columns should be converted to numbers, it seemed easier to just convert the proper DataTable columns from string to number, so that’s what I ended up doing.

1 Like

How do I just “chop off” the decimal?

Hi @David_MacKenzie,

You have a few options depending on where you are working with these numbers within the Losant platform.

Within the visual workflow engine you can use the Math Node to perform a truncating function on the value (https://docs.losant.com/workflows/accessing-payload-data/#supported-functions).

You can also leverage the format helper guide to help with accessing and manipulating values from your payload. https://docs.losant.com/workflows/accessing-payload-data/#format-helpers.

For your specific use, the option {{ format num .2 }} will result in the number formatted without the decimals.

Let us know if we can help further.

Thanks,
@Aidan_Zebertavage

@David_MacKenzie,

A few more specifics regarding the {{ format num '.2' }} to clarify its use:

The .2 does need to be in quotes: ‘.2.’ , as seen above.
The .2 refers to the number of significant digits to be reported, so a value like 1.2 will still result in 1.2.

To completely remove the decimal values, you could use fixed point notation: {{format num '.0f'}}

We have this linked in the Losant docs but more information on D3 formatting can be found here: https://github.com/d3/d3-format#locale_format.

Thanks,
@Aidan_Zebertavage

Thanks. I am too slow to figure out how this works.

I have an attribute that is a number type called TotalLenTravel. See the block/column below:

image002.jpg

I am trying to format in cell template box below:

If I put what you suggested, nothing is displayed in table. Same with {{format value .2}}. Sadly, I am one of THOSE users….

@David_MacKenzie,

No problem at all! We are happy to help. Take a look at the attached screenshot here, I think we may just have some syntax issues in your cell template. When specifying the format type, you need quotes and the decimal point, no comma needed.

In my example I used {{format value '.0f' }} which took a simulated value for total length traveled of 600.754937 and truncates it down to 601 in the data table on the dashboard.

Let me know if this solves the issue on your end.

Thanks,
@Aidan_Zebertavage

Yes, that is exactly what I was looking for. Thank you.