user1058182
user1058182

Reputation: 27

Format a number in C# from left side

I want to format my number from the left side.

Suppose I have 00.524, then it should give me 0.524.

If I have 10.524, then it should convert it to 10.524.

This means if I have 0 at the start, I should remove the zero. Is there any string format like 0:n2 for this?

Upvotes: 1

Views: 229

Answers (3)

Fischermaen
Fischermaen

Reputation: 12458

Assuming you'll always have a valid double value, you can do that:

string.Format("{0:n2}", double.Parse(stringWithNumber));

Upvotes: 2

Joel Etherton
Joel Etherton

Reputation: 37533

You'll need to use a custom format specifier like:

myDouble.ToString("#0.###")

Make sure to use a # where you want a number to appear only if it is valid and a 0 to appear anywhere you want a zero to appear otherwise.

Upvotes: 1

Shai
Shai

Reputation: 25619

String.Format("{0:0.000}", 00.524);

More on this here

Upvotes: 1

Related Questions