javascriptdocxtemplater

Docxtemplater - using multiple filters | Country-specific format and custom decimals


Is there a way to apply multiple filters in docxtemplater?

I would like to apply a "toLocaleNumber" filter as well as a "toFixed" filter.

Example: Input for variable 'Price': 500.50

In the document it says "EUR {price | toLocaleNumber: 'de-de'}" and the output would be "EUR 500,5".

It's obviously missing the last 0. If the input was 500, I would also like to show two decimals to have 500,00. I found the toFixed function that would take care of it, but I'm not sure how to combine two filters on the same variable.

Any ideas?

Many thanks, Jenny


Solution

  • Not sure how helpful (or new) this is for anyone, but just in case I would like to share my solution.

    Within our toLocaleNumber function we use toLocaleString(locale). I first tried adding a step before that and applied the toFixed function to the original number input, missing the fact that that function returns a string which then of course causes toLocaleString to throw an error.

    I then found that toLocaleString already has an option parameter called "minimumFractionDigits", which nicely adds two trailing zeros even if the input number doesn't have any decimals.