Add a leading Zero


#1

I am having issues on adding a leading 0 ( Depends how many 0) if the string does now have specific length.( Lets say 12).
If the string has a length of 10 it will add 2 leading 0 in front of the string.
I am half way through it and my if/else condition is not reading the condition.
Help would be appreciated!
:slight_smile:


#2

@sherpa24, use this

Stringvariable.padleft(12,CChar(“0”))

Regards,
Dominic :slight_smile:


#3

I believe something like this will also work.

stringVariable.Concat(stringVariable, “0”)

This option is dynamic for any string variable.

edit: Ah, misread your question.


#4

I put my condition as dt_str.Length <=11 but it won’t give me an error but it is not working ( in a sense not adding leading 0 even if the number is lessthan 12)


#5

sorry it works now, didn’t see you edited your last post, appreciate you brother!!!


#6

Hi, I suggest using
.PadLeft(12,“0”) like suggested above, cause that will add the zeros up to a total of 12 characters, so no conditions are needed to check the length.

Also, you can just format it with .ToString.
CInt(text).ToString(“000000000000”)


#7

Hi,

Please refer the below tutorials,

thanks,
Karthik