add leading zeros to number

823 views
Skip to first unread message

Jevon, Graham

unread,
May 5, 2022, 10:24:29 AM5/5/22
to openr...@googlegroups.com

Hi,

 

Does anyone know the expression for adding leading zeros to a number? (i.e. convert 1 to 001 and 12 to 012).

 

I've done this before, but can't remember the expression or find it in the user guide.

 

At  the moment I have a string so my partial expression is value.toNumber()

 

Thanks, Graham


 
******************************************************************************************************************
Experience the British Library online at www.bl.uk
The British Library’s latest Annual Report and Accounts : www.bl.uk/aboutus/annrep/index.html
Help the British Library conserve the world's knowledge. Adopt a Book. www.bl.uk/adoptabook
The Library's St Pancras site is WiFi - enabled
*****************************************************************************************************************
The information contained in this e-mail is confidential and may be legally privileged. It is intended for the addressee(s) only. If you are not the intended recipient, please delete this e-mail and notify the postm...@bl.uk : The contents of this e-mail must not be disclosed or copied without the sender's consent.
The statements and opinions expressed in this message are those of the author and do not necessarily reflect those of the British Library. The British Library does not take any responsibility for the views of the author.
*****************************************************************************************************************
Think before you print

Parthasarathi Mukhopadhyay

unread,
May 5, 2022, 1:12:36 PM5/5/22
to openr...@googlegroups.com
Dear Graham

In a similar case for us where we need to convert the ID column (1 to 6831) with a prefix (3 alphabets) and 6 places of numbers (like  hlv-000001 to hlv-006831) we have used the following GREL:

"hlv-"+slice("00000"+(rowIndex+1),-6)

Best

--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openrefine/CWXP265MB2951FA140FA21BC70B7899E382C29%40CWXP265MB2951.GBRP265.PROD.OUTLOOK.COM.

Adam Gray

unread,
May 9, 2022, 8:16:52 AM5/9/22
to OpenRefine
Hi Graham,

Another option may be a variation of this expression, which I've used to pad item identifiers with leading zeros: 

"000"[0,3-value.length()] + value

All credit to University of Texas non-vertebrate Paleontology Lab tutorial (2013!), which I access here: https://wikis.utexas.edu/pages/viewpage.action?pageId=46631837

Owen Stephens

unread,
May 9, 2022, 9:43:57 AM5/9/22
to OpenRefine
Hi Graham,

You can do this with number to string formatting so if you start with the strings "1" and "12" you could use:

value.toNumber().toString("%03d")

It will result in 
1 -> 001
12 -> 012

The documentation for how this works is pretty sparse as it's based on the underlying Java format code which is pretty obscure.
The first part of this document is relatively helpful https://docs.oracle.com/javase/tutorial/java/data/numberformat.html (up to the section on "The DecimalFormat Class" which doesn't apply), and there are lots of examples given in this document https://www.baeldung.com/java-number-formatting

You might be able to find some further helpful examples by googling tutorials on using java format or formatting numbers to strings in java

Best wishes

Owen

Thad Guidry

unread,
May 9, 2022, 9:58:35 AM5/9/22
to openr...@googlegroups.com
In our docs here GREL functions | OpenRefine we provide a link as shown below to the Formatter class which fully describes all the options available to use.  The tutorial Owen provided covers some of the common uses, but it's quite extensive to do many kinds of formatting conversions.  It's a language in itself!

You can use toString() to convert numbers to strings with rounding, using an optional string format. For example, if you applied the expression value.toString("%.0f") to a column:



Owen Stephens

unread,
May 9, 2022, 10:01:40 AM5/9/22
to OpenRefine
On Monday, May 9, 2022 at 2:58:35 PM UTC+1 Thad Guidry wrote:
In our docs here GREL functions | OpenRefine we provide a link as shown below to the Formatter class which fully describes all the options available to use.  
Honestly I find the Java documentation on the formatter class pretty much impossible to use - and I speak as someone who is happy writing code. I almost always have to google for examples or tutorials to find the right incantation for a particular outcome.


Thad Guidry

unread,
May 9, 2022, 10:09:53 AM5/9/22
to openr...@googlegroups.com
hehehe, yeah understood, I'm just used to it resembling the Fortran alternates back in the days on college DEC's as a wee. :-)

--
You received this message because you are subscribed to the Google Groups "OpenRefine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openrefine+...@googlegroups.com.

Antoine Beaubien

unread,
May 9, 2022, 12:53:50 PM5/9/22
to OpenRefine
I personally like this simple formula that I use in every language:
("000"+ "123").slice(-4)

"123" being the number or sting number I want to pad. If I want a 4 digits number, I add 3x "0" (3x because 4 -1 = 3, I take for granted I never have an empty string, otherwise it's 4), and cut from the right to keep 4 digits.

Very visual and easy to remembre.

Regards, Antoine

Jevon, Graham

unread,
May 11, 2022, 3:54:54 AM5/11/22
to openr...@googlegroups.com

Thanks Thad, Owen, Parthasarathi

 

("%.0f") is exactly the expression I was looking for. I didn’t think to consider it would be related to .toString.

Reply all
Reply to author
Forward
0 new messages