简体   繁体   中英

How to align floating point numbers in C# (left padding)

I have the following floating point numbers, all with 2 decimal places:

2.47
57.83
93.92
119.20

I want to output these numbers as follows:

  2.47
 57.83
 93.92
119.20

How can I achieve that in C#, if the font is not monospaced?

Edit:

Or is there any invisible character that occupies the same amount of horizontal space as a digit?

试试string.Format("{0,6:F2}", number)

I've never used Gtk# before, but a quick Google search revealed this .

label.Justify = Justification.Right;

This is a much simpler and more reliable strategy than trying to manipulate the string to the proper width.

我的解决方案是使用图形空间填充(U + 2007),其宽度等于数字的宽度:

label.Text = number.ToString("0.00").PadLeft(6,'\x2007');

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