简体   繁体   中英

Output floating point width and precision C#?

I'm moving some C++ code to C# and need formatted tabular information that has floating point numbers. Is there a way to duplicate printf("%f8.2",fNumber); where both width and precision are honored in a single call?

float f1 = 12345.67; 
float f2 =    45.67; 
Console.Output("{0:F2}",f1.PadLeft(8)); // works, but isn't a single call
Console.Output("{0:F2}",f2.PadLeft(8));

// desired output
12345.67
   45.67

This will display your numbers to width of 8 and width of two decimal places:

Debug.WriteLine("{0,8:F2}", f1);

You can find relevant documentation here .

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