String formatting: negative/positive floating poin

2019-06-25 01:05发布

How can I use String.Format in C# so doubles are displayed like this:

example:
___-1.000
____1.000
__100.123
-1000.321
_1000.214

etc...

Where _ is space (" ");

All I can do is String.Format("{0:F3}", -123.321);

2条回答
爷、活的狠高调
2楼-- · 2019-06-25 01:22

You can use alignment:

String.Format("{0,10:F3}", -123.321)

where 10 is preffered length.

See Composite Formatting.

查看更多
等我变得足够好
3楼-- · 2019-06-25 01:31

Found a quick article, in short:

String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

Source: Here <- Look here for more.

查看更多
登录 后发表回答