【问题标题】:String formatting: negative/positive floating point numbers字符串格式:负/正浮点数
【发布时间】:2010-12-04 22:31:17
【问题描述】:

我如何在 C# 中使用String.Format,这样双打显示如下:

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

等等……

其中 _ 是空格 (" ");

我能做的就是String.Format("{0:F3}", -123.321);

【问题讨论】:

    标签: c# .net string double string-formatting


    【解决方案1】:

    你可以使用对齐方式:

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

    其中 10 是首选长度。

    Composite Formatting

    【讨论】:

      【解决方案2】:

      找到一篇快文,简而言之:

      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    "
      

      来源:Here

      【讨论】:

        猜你喜欢
        • 2020-09-06
        • 2017-12-08
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多