String formatting: negative/positive floating point numbers

徘徊边缘 提交于 2019-12-10 17:24:02

问题


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);


回答1:


You can use alignment:

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

where 10 is preffered length.

See Composite Formatting.




回答2:


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.



来源:https://stackoverflow.com/questions/4356185/string-formatting-negative-positive-floating-point-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!