Display numbers comma separated

I am using $view widget to display content of a field that contains just a number. I want to display the number comma separated.

So, the number in the field is ‘123456’ but I want to display ‘123,456’.

How do I achieve that?

More generally, how do I control display of numbers? (Like specifying number of decimal places, use of commas, rounding off to the nearest integer, etc.)

1 Like

A quick answer before my bed time. Get that number into a filter an you can use the mathematics operators to round, set decimal places and more. maths operators

You can, I am sure, write a filter to add the commas, I have before, but do not recall right now.

@TW_Tones Thanks. That gave me a clue.

<$text text={{{ [{!!amount}split[]butlast[3]join[]] [{!!amount}split[]last[3]join[]] +[join[,]] }}} />

is a very rudimentary solution and will work only till six digit numbers. But serves the purpose.

1 Like

There’s probably a more compact way of doing this, but here’s a really long-winded solution:

\define reverse() [addsuffix<accumulator>]

<$text text={{{[<number>split[]reduce<reverse>split[]] :reduce[<index>add[1]remainder[3]match[0]then<currentTiddler>addsuffix[,]else<currentTiddler>addprefix<accumulator>] +[split[]reduce<reverse>trim:start[,]]}}} />

change the myNumber to a field.

What this does is:

  • gets the number, splits it into characters, reverses the order and joins them together, then splits it again
  • if the index+1 of this character is a multiple of 3, add a comma.
1 0 3 4 5 | original number
5 4 3 0 1 | reversed number
-----------------------------
0 1 2 3 4 | index
1 2 3 4 5 | index + 1

after this step

5 4 3 , 0 1 
  • split the resulting number into characters, reverse it again, then join them back. if the final number has a “,” at the start, remove it.
2 Likes

Hello,

Give this a spin:

\define comma-separate-num() (\d)(?=(\d{3})+(?!\d))

{{{ [[234567891]search-replace:g:regexp<comma-separate-num>,[$1,]] }}}

The regular pattern above from Number formatting in JavaScript (Tom’s Blog).

Don’t ask me how that works. I’m very new to regular expressions.

3 Likes

\define comma-separate-num() (\d)(?=(\d{3})+(?!\d))

In Regexp, certain symbols have operator functions, much like the TW Filter Syntax. The parenthesis are “groups”, and things like ?= and ?! at the start of those groups are “look-aheads”. Your Regex (GOOD FIND) says:
Find a Digit (\d), and look forward for another set of groups (?= , the first of which is a set of 3 digits after it (\d{3}), and find as many of those as you can +, BUT if you find a single digit after the set of 3, discard the capture group (?!\d) finally, end the parent group ).

This explanation, and checking validity can be done at https://regexr.com/

1 Like

@sull-vitsy Thanks. I tried your solution replacing with {!!amount} but that does not give the desired results. Am I doing something wrong?

@Charlie_Veniot Thanks! This works perfectly.

One more question. Can I not define something such that I just pass on the name of the field and it returns the comma separated number?

So, instead of having to write:

<$text text={{{ [{!!amount}search-replace:g:regexp<comma-separate-num>,[$1,]] }}} />

I will write

<$comma-separated (amount)>

I am sure, I am getting the syntax wrong. But hope, the idea is clearly expressed.

You can do it with a macro. I can’t help you more because I post from mobile.

For sure !

Sorry, just at the tail end of a coffee break and I must get back to work.

I’ll send details about macro later if somebody else hasn’t yet answered.