A QGIS question:

How can I insert commas in 4 digit numbers in an attribute table that has multiple records, with some numbers that are only in the hundreds? It's for an animation so multiple labels are being displayed.

here's a snapshot of a section of the attribute table I'm talking about

I tried using the format_number option under the string menu in field calculator, but it only works when I enter a specific number in the expression dialogue, so i imagine taht this tool only works for attribute tables with one record.

I don't know much about coding so hopefully there's an option that doesn't need that, or a simple way to describe how to do the coding.

I don;t think this is the same as the topic "Forcing decimal numbers to use comma in ArcGIS Field Calculator? " because i still want to keep the decimal places.

  • The best way to do this is with an advanced field calculation: turn the number into a string, starting from the right group the digits into 3 and then join with a comma... it's not difficult but if you have no coding ability or experience it could seem quite daunting. How do you feel about Advanced Field Calculations? Jun 16, 2015 at 22:38
  • Thank you for the quick reply! I'm willing to try something new but I'm not sure how to do an Advanced Field Calculation. is there anyway you could describe some more specific steps please? Jun 16, 2015 at 22:46
  • Welcome to GIS SE! As a new user be sure to take the Tour. Would you be able to edit your question to mention whether you are using ArcGIS for Desktop or QGIS (and which version) because there is much overlap in terminology between these and other desktop GIS products.
    – PolyGeo
    Jun 16, 2015 at 22:57
  • Can you insert an example of the expression you wrote with format_number? Jun 17, 2015 at 10:12
  • I don't think this is a duplicate because the earlier question used ArcGIS for Desktop while this is for QGIS. Their field calculators are sufficiently different that an answer to one will need considerable adapting before it can be used with the other.
    – PolyGeo
    Jun 17, 2015 at 10:13

1 Answer 1


In the advanced field calculation be sure to select python as the syntax: enter image description here

The code (to copy and paste):

def FormatThis(number):
    AsString = str(int(number)) # just the whole numbers
    Dec = number-int(number)    # extract the decimals
    G3 = [AsString[::-1][i:i+3][::-1] for i in range(0, len(AsString), 3)][::-1]
    return ','.join(G3) + str(Dec)[1:5] # 5 is 3 decimal places

Now, let's go into how this works..

Firstly, you've selected python in the field calculator, then select show codeblock to bring up the window looking like the image. The first (big) window is where you write your routine(s) with def functioname(Input) the second is where you call it with your field (note the exclamation marks).

The code: I grabbed the 'group by 3 from' a Stack Overflow post (thanks georgesl), so first you make the whole numbers into a string and save the decimals then break into groups and format back with commas (','.join(G3) takes the elements in G3 and puts them into a string with commas in between).

  • thank you! I am leaving and will not be able to try this until tomorrow, but I'll let you know whether I'm able to figure this out! Jun 16, 2015 at 23:50
  • The question was specifically for QGIS, yet this answer is an ArcMap specific field calculator operation. I believe the QGIS solution will be to convert your integer/real field to a string and then use the format_number function.
    – cbunn
    Jun 18, 2015 at 13:14
  • Yes @cbunn, the QGIS edit came after I posted the answer. In QGIS 2.8+ there is the ability to enter your own functions see lutraconsulting.co.uk/blog/2015/06/05/qgis-function-editor however this post has been considered as a duplicate so further modifications seem a little redundant. Jun 18, 2015 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.