Mathlight
Mathlight

Reputation: 6653

What is the C# equivalent to Delphi's FormatFloat?

I'm porting some Delphi code to C#. I can't find a similar function to Delphi's FormatFloat.

I've got this line of code in Delphi

str := FormatFloat('000', 1);

which assigns to str the string '001'. Note the leading zeros.

How can I achieve the same result in C#?

Upvotes: 0

Views: 1238

Answers (2)

Antonio Bakula
Antonio Bakula

Reputation: 20693

You can use ToString() method :

int number = 3;
string fmt = number.ToString("D3");

string variable fmt will have value "003"

Upvotes: 2

akton
akton

Reputation: 14386

You use string.Format() with custom numeric format strings. For example:

int a = 1;
string.Format("{0:000}", a); // returns "001"

Upvotes: 5

Related Questions