# add integers to a Numeric field

913
4
12-21-2022 12:44 PM
by
Emerging Contributor

1.                      2.

I need all of the values in example one to have 10 integers like the ones one table two. To do So I need to add the necessary amount of ceros before the value.  there are values that need 5, 4, or 1 cero to be added. Any ideas of geoprocessing tools that do this or how to do it by calculating the field ?

Thanks!!!!

same numeric values but the numbers cave ceros in front.

1 Solution

Accepted Solutions
Frequent Contributor

Hi,

Doing this in field calculator is definitely the best option! Is the AccountID field a text field? Because if it's an integer field you won't be able to add zeros onto the front, and in that case you would need to add a text field to calculate the values into.

As for some code to do what you're trying to do, if you put this in the code block using Python3:

``````def addZeros(ID):
ID = str(ID)
noZeros = 10 - len(ID)
return "0" * noZeros + ID``````

and then the following as the python expression to run above the code block box

``addZeros(!IDNum!)``

With the !IDNum! being the Account ID field, that should do what you're after!

Cheers,

Josh

4 Replies
Frequent Contributor

Hi,

Doing this in field calculator is definitely the best option! Is the AccountID field a text field? Because if it's an integer field you won't be able to add zeros onto the front, and in that case you would need to add a text field to calculate the values into.

As for some code to do what you're trying to do, if you put this in the code block using Python3:

``````def addZeros(ID):
ID = str(ID)
noZeros = 10 - len(ID)
return "0" * noZeros + ID``````

and then the following as the python expression to run above the code block box

``addZeros(!IDNum!)``

With the !IDNum! being the Account ID field, that should do what you're after!

Cheers,

Josh

by
Emerging Contributor

Thanks!!! This worked.

MVP Esteemed Contributor
``````# == example
a = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789]
frmt = "{:010d}"

[frmt.format(i) for i in a]

['0000000001',
'0000000012',
'0000000123',
'0000001234',
'0000012345',
'0000123456',
'0001234567',
'0012345678',
'0123456789']

# --- field calculator useage

"{:010d}".format(!YourFieldWithNumbers!)``````

... sort of retired...
by
MVP Esteemed Contributor

An easy way using Arcade uses Text

``````var num = 1234;
return Text(num,'0000000000');``````