Convert string to int and keep the leading zero

Hello Everbody,

I have an issue when I want to convert string into integer. I get text from a Mainframe and the text is stock into a string variable.
I have my str_var as str_FirstPg = “001”
I want to convert into int and I did like this :
CInt(str_FirstPg), it returns 1. But I need to have the final result as 001.

How can I keep the 00, when I convert into an integer ?
Any idea, please ?

Thank you all !

Hi @najoua.abbaci

Integer is not able to store leading zeros in any way, but if you are to display or convert your integer back to string you can either store the length of the str_FirstPg in order to display/convert into right length of leading zeros.

If the length is fixed you only need to add leading zeros without storing the length by it self :slight_smile:

Use padleft to add leading zeros :
Stringvariable.padleft(3,CChar(“0”))

1 Like

Thank you @BjarteIversen

I needed to have the int with the format “001”, but if it is not possible (and thank you for this information). I will keep the “1” format.

1 Like

Convert the variable to integer first, then format the integer to string with something like FinalValue = YourIntVariable.tostring(“000”). Note this end result variable will be a string.

If you need the variable as an integer for some process, but need the 001 for a display, then just use two variables one for calculations and one for display.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.