Number formatting

Is there a widget/macro that formats big numbers? E.g. I put in 1234567890 and it renders as 1,234,567,890, or I add a flag and the above renders as 1.234G

There are macros around, I even wrote them in the past, but they can be fiddly.

However the fact is there should be JavaScript macros that can easily accept a number and format it as such, this would make a good example for the use of a such code in a filter operator module.

I will see what I can find/write, hopeful someone has already done it.

Evans Formula plugin did this from memory but that may be overkill.

Regular expressions can normaly be used to do this

Number formatting is language specific.

We most likely will need a new format operator suffix format:number:de-DE because

[[12345678.99]format:number:de-DE[]] should return 12.345.678,99 and
[[12345678.99]format:number:en-US[]] should return 12,345,678.99

or something similar.

From my point of view we would need 2 parameters. Because if I have a number in a field eg: 12,3 it means 12.3 for others. So if the number already contains is , or . it’s not clear anymore, what it is, without an indicator, how the comma or dot should be interpreted

see: Intl.NumberFormat() constructor - JavaScript | MDN

1 Like

Alternatively we could provide the decimal point and thousands seperators as parameters.

I have already used Chat Gpt to add a function to them math.js called thousands and yes only commas.

Add this after “use strict” in the tiddler $:/core/modules/filters/math.js save and reload

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(".");
    }
);

use ...[thousands[]] after the result.

A more robust solution is needed as raised by @pmario

Circling back to this thread as I had the same need. I just extracted the fixed function from the core and added the bit for us-EN formatting (hard-coded) after the hint from pmario. I’m don’t actually know Javascript, just copy/pasted this together, but it seems to be working. Just posting here in case somebody looks this up after me.

Works like this:
image

$__stobot_filters_currency.js.json (1.1 KB)

2 Likes