Crystal Reports in need of padding

I don’t write that many Crystal Reports but when I do it is a refreshing change. They integrate nicely with Raiser’s Edge and as I showed with my previous post you can write code that will open up a Crystal Report and run it without having to run an export first. You can use any data source instead of a Raiser’s Edge export. The formula language is quite intuitive if you have worked with VBA, VB6 or VB.NET (and simple enough for those with any other mainstream language experience). However, the other day I need to pad a number with some leading zero. In VB.NET there is the String.LeftPad method which works really well. No such luck with Crystal.

My number could be one, two, three or four characters in length. If it was less than four then zeros should be added in front. In VB.NET you would simply write:


There are of course lots of ways you could do this. You could determine how many characters there are in myNumberField and add differing numbers of zeros to the value but this is clumsy and a lot of extra code.

Instead I did this which is quite succinct:

RIGHT("0000" & {myNumberField}, 4)