Help making a standalone maths operator to format thousands

Folks,

As a Java Script Kiddy. I need some assistance from someone with Javascript in tiddlywiki skills.

I have a working filter operator called thousands. I am not sure where I obtained the JS code to do this but I added it to $:/core/modules/filters/math.js and it works well $__core_modules_filters_math.js.json (7.0 KB)

  • eg {{{ [[1000345.3]thousands[]] }}} produces 1,000,345.3.

However I would like to turn this into a standalone filter operator module, rather than overwrite the core modual.

I placed the following in its own tiddler but it is not working. I believe it needs access to other functions found in $:/core/modules/filters/math.js

exports. Thousands = makeNumericBinaryOperator(
    function(a) {
        // Splitting the number into integer and fractional parts
        var parts = a.toString().split(".");
        // Formatting the integer part with commas
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        // Concatenating the integer and fractional parts back together
        return parts. Join(".");
    }
);

Further I am aware that other “languages” use the opposite to us in Australia, So wonder how to parameterise this or obtain these from localisation.

  • eg {{{ [[1000345.3]thousands[]] }}} would produce 1.000.345,3.

Some assistance would be appreciated.

The thousand marker is language specific. See: Intl.NumberFormat - JavaScript | MDN

You should have a look at filter operator, that only contains 1 operator. It will show the whole construction needed to make it work. eg: contains.js

But I think it should be an element of the filter operator and it should have some the options that are described in the link. So eg:

  • [[12345.6]format:currency[de-DE],[EUR]] or
  • [[12345.6]format:number[de-DE]] or

something like this. I am not sure without testing it.