简体   繁体   中英

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#?

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

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

You can use ToString() method :

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

string variable fmt will have value "003"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM