【问题标题】:Dynamic number format in .NET?.NET 中的动态数字格式?
【发布时间】:2011-04-12 15:58:07
【问题描述】:

我有一个问题,找不到解决方案。我有数字(十进制),例如 85.12343 或 100 或 1.123324。我想以这样的方式格式化它,结果总是 13 个字符长,包括分隔符。

100          --> 100.000000000
1.123324 --> 1.12332400000

我尝试使用 toString,但失败了。我怎么能这样做?

谢谢:)

【问题讨论】:

  • 123456789.987654321 呢?你会期待123456789.988吗?
  • 正确,但在分隔符之前不会大于 100。
  • 类似问题,通过以下链接解决stackoverflow.com/a/3717559/3300028

标签: c# number-formatting


【解决方案1】:
int digits = 13;
decimal d = 100433.2414242241214M;
int positive = Decimal.Truncate(d).ToString().Length;
int decimals = digits - positive - 1; //-1 for the dot
if (decimals < 0)
    decimals = 0;
string dec = d.ToString("f" + decimals);

在需要时,它不会从整个部分中删除数字,只会删除部分。

【讨论】:

  • +1 因为知道合适的格式化程序,我永远记不住除了“d”之外的任何格式化程序。
  • 这很好,但是不四舍五入就可以工作吗?
【解决方案2】:

我会选择Kobi's answer,除非您的开头可能多于 13 位数字,在这种情况下您可能需要做这样的事情(警告:我什至没有尝试来提高效率;当然,如果需要,可以通过一些方法对其进行优化):

public static string ToTrimmedString(this decimal value, int numDigits)
{
    // First figure out how many decimal places are to the left
    // of the decimal point.
    int digitsToLeft = 0;

    // This should be safe since you said all inputs will be <= 100M anyway.
    int temp = decimal.ToInt32(Math.Truncate(value));
    while (temp > 0)
    {
        ++digitsToLeft;
        temp /= 10;
    }

    // Then simply display however many decimal places remain "available,"
    // taking the value to the left of the decimal point and the decimal point
    // itself into account. (If negative numbers are a possibility, you'd want
    // to subtract another digit for negative values to allow for the '-' sign.)
    return value.ToString("#." + new string('0', numDigits - digitsToLeft - 1));
}

输入/输出示例:

输入输出 -------------------------------------- 100 100.000000000 1.232487 1.23248700000 1.3290435309439872321 1.32904353094 100.320148109932888473 100.320148110 0.000383849080819849081 .000383849081 0.0 .000000000000

【讨论】:

    【解决方案3】:

    快速'n'脏:

    return (value.ToString("0.#") + "0000000000000").Substring(0, 13);
    

    【讨论】:

    • 不错,但它不会对数字进行四舍五入。
    【解决方案4】:
    string formatted = original.ToString("0.000000000000").Remove(13);
    

    【讨论】:

      【解决方案5】:

      除了简单地填充字符串之外,您还可以做一些更精细的数学运算来确定位数:

      String FormatField(Int32 fieldWidth, Decimal value) {
        var integerPartDigits =
          value != Decimal.Zero ? (int) Math.Log10((Double) value) + 1 : 1;
        var fractionalPartDigits = Math.Max(0, fieldWidth - integerPartDigits - 1);
        return value.ToString("F" + fractionalPartDigits);
      }
      

      请注意,如果该值为负数或具有比字段宽度少一位的整数部分,您将无法获得所需的结果。但是,您可以根据您希望如何格式化和对齐这些数字来修改代码以适应这些情况。

      【讨论】:

      • +1 - 我发布的内容几乎相同。虽然没有使用log,但decimal 似乎很奇怪。
      【解决方案6】:

      怎么样

      string newString;
      if (original.ToString().Length >= 13)
      {
          newString = original.ToString().Substring(13);
      }
      else
      {
          newString = original.ToString().PadRight(13, '0');
      }
      

      【讨论】:

        【解决方案7】:
        int noofdecimal=3;
        double value=1567.9800
        value.ToString("#." + new string('0', noofdecimal));
        
        //Result=1567.980
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多