Nano HE
Nano HE

Reputation: 9297

Prefix '0' to the int variable which less than '10', How?

Is there any simple method to concatenate '0' before an int variable.

Like:

int i = 2;

// produce    
i = someMethod(i);

// output:
i = 02

Upvotes: 1

Views: 330

Answers (1)

willvv
willvv

Reputation: 8639

If you mean "concatenate", then you can define someMethod() as follows:

string someMethod(int i){
  return string.Format("{0:d2}", i);
}

The "2" in the string format defines the number of characters in the output.

Upvotes: 5

Related Questions