Sankarann
Sankarann

Reputation: 2665

Apply string format form left to right

How can you make a string that is formatted from a value from left to right?

string.Format("{0:00-00-0000}", 123);

The above returns as 00-00-0123 I would like it to be 12-30-0000

Any idea to achieve this?

Upvotes: 7

Views: 301

Answers (2)

Jonathas Costa
Jonathas Costa

Reputation: 1016

Try this:

var padded = long.Parse((123).ToString().PadRight(8, '0'));
string.Format("{0:00-00-0000}", padded);

Upvotes: 5

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12797

Another crazy idea is to do some math

int i = 123;
string.Format("{0:00}-{1:00}-{2:0000}", i / 1000000, (i / 10000) % 100, i % 10000);

Upvotes: 0

Related Questions