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.)
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.
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 filter leverages the revIndex variable of the :map filter run prefix to insert a comma when the index of a character (counted from the end) modulo 3 is 2.
[edit]: added a second :map filter run to remove the leading comma when input length is a multiple of 3.
[edit 2]: replaced the second :map filter run by trim[,]