DarkPixel
DarkPixel

Reputation: 246

Format int with negative number

I have file that match this pattern:

Name-003
Name-002
Name-001
Name0000
Name0001
Name0002

And I need a way to format a string to have this pattern.

If I use

string.Format("Name{0:0000}", index)

It give me:

Name-0003
Name-0002
Name-0001
Name0000
Name0001
Name0002

I need a way to specify the number of digit including the negative sign character. I want a solution with String.Format()

Upvotes: 0

Views: 217

Answers (1)

Dan Puzey
Dan Puzey

Reputation: 34198

You could look at the documentation for numeric formats (specifically the ; character):

string.Format("Name{0:0000;-000}", index)

... or you could learn how to use an if statement to select between two format strings based on the value of index.

Upvotes: 3

Related Questions