简体   繁体   中英

String formatting: negative/positive floating point numbers

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

You can use alignment:

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

where 10 is preffered length.

See Composite Formatting .

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.

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