Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add leading zero to a text field in crystal reports

I'm trying to think of a way to add a leading zero to a string field value. For example I have 12345 but need a formula that would convert it to 012345. I'm new to Crystal as I know that this is probably a simple formula but cant seem to get it to work.

12345 => 012345  (add leading zero to make it 6 chars)

Thanks in advance.

like image 270
Dagz200 Avatar asked Aug 13 '12 20:08

Dagz200


2 Answers

Try this

totext(your_number, "000000");

1st arg.: Well, it's the input.

2nd arg.: Number of digits you need in the output.

For ex,

num = 1234;

totext(num, "000000");

Output:

001234

And, if you want to add a fixed number of zeroes, then you can just use (to add 3 leading zeroes):

"000" + totext(your_number, 0); // to add 3 leading zeroes

Note: The final output is a string not a number.

like image 161
Bhaskar Avatar answered Dec 24 '22 03:12

Bhaskar


To pad a numeric string value with zeroes to a certain length:

local numbervar yournum := tonumber({table.your_string}); //convert to number
totext(yournumnum, '000000') //convert back to padded string of length 6

OR for a universal string

local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length

if length(yourstring) >= LENGTH then yourstring else
  replicatestring('0',LENGTH-length(yourstring)) + yourstring
like image 23
Ryan Avatar answered Dec 24 '22 03:12

Ryan