字符串格式问题

发布于 2024-08-18 19:26:51 字数 1267 浏览 8 评论 0原文

我有以下方法:

public static string ReturnFormat(string input, int maxLength, int decimalPrecision, char formatChar)
  {
   string[] format = new string[2];
   string[] inputs = new string[2];

   inputs = input.Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);

   if (input.Length > maxLength)
   {
    int offset = 0;
    int counter = 0;

    if (inputs[0].Length > maxLength - (1 + decimalPrecision))
    {
     offset = maxLength - (1 + decimalPrecision);
    }
    else
     offset = inputs[0].Length;

    for (int i = 0; i < offset; i++)
    {
     format[0] += formatChar;

     if (counter < decimalPrecision)
     {
      format[1] += '0';
      counter++;
     }
    }

    System.Windows.Forms.MessageBox.Show("{0:" + format[0] + "." + format[1] + "}");
    return String.Format(CultureInfo.CurrentCulture, "{0:" + format[0] + "." + format[1] + "}", input);
   }
   else
    return input;
  }

说我正在使用 as:

ReturnFormat("12.3456789011243", 10, 2, '#') // format is {0:##.00} // output 12.3456789011243
ReturnFormat("12345678901.1243", 10, 2, '#') // format is {0:#######.00} // output 12345678901.1243

现在我的问题是输入字符串的格式不正确,但格式 strig 似乎没问题。 对我做错了什么有什么想法吗?

I've got the following method:

public static string ReturnFormat(string input, int maxLength, int decimalPrecision, char formatChar)
  {
   string[] format = new string[2];
   string[] inputs = new string[2];

   inputs = input.Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);

   if (input.Length > maxLength)
   {
    int offset = 0;
    int counter = 0;

    if (inputs[0].Length > maxLength - (1 + decimalPrecision))
    {
     offset = maxLength - (1 + decimalPrecision);
    }
    else
     offset = inputs[0].Length;

    for (int i = 0; i < offset; i++)
    {
     format[0] += formatChar;

     if (counter < decimalPrecision)
     {
      format[1] += '0';
      counter++;
     }
    }

    System.Windows.Forms.MessageBox.Show("{0:" + format[0] + "." + format[1] + "}");
    return String.Format(CultureInfo.CurrentCulture, "{0:" + format[0] + "." + format[1] + "}", input);
   }
   else
    return input;
  }

Which say I'm using as:

ReturnFormat("12.3456789011243", 10, 2, '#') // format is {0:##.00} // output 12.3456789011243
ReturnFormat("12345678901.1243", 10, 2, '#') // format is {0:#######.00} // output 12345678901.1243

Now my issue is that the input string is not formatted well, still the format strig appears to be ok.
Any ideas of what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-08-25 19:26:51

您的输入是字符串而不是双精度数,因此它的格式类似于字符串:在这种情况下,格式不知道小数位。

您可以使用 Double.Parse() 将字符串转换为 Double 值,但要注意使用正确的区域性。

另一件事是,在这两种情况下不使用更自然的格式 {0:0.00} 是否有具体原因?如果您确实想使用数字占位符,那么 # 就可以,否则 0 最好。

经过测试的解决方案(注意它会截断并且不会四舍五入)我需要一些时间来了解实际想要的内容:

public static string ReturnFormat(string input, int maxLength, int decimalPrecision)
{
    if (input.Length <= maxLength)
        return input;
    Char separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
    string[] inputs = input.Split(separator);
    // NB: truncating rather than rounding
    if (inputs[1].Length > decimalPrecision)
        inputs[1] = inputs[1].Substring(0, decimalPrecision);
    int digits = (maxLength - decimalPrecision - 1);
    // NB: truncating rather than rounding, adding ~ to signalize the
    // presence of missing significant digits
    if (inputs[0].Length > digits)
        inputs[0] = inputs[0].Substring(0, digits-1) + "~";
    return inputs[0] + separator + inputs[1];
}

Your input is a String not a Double, so it gets formatted like a string: the formatting does not know about decimal places in that case.

You could use Double.Parse() to transform the string into a Double value, but take care of using the right culture.

Another thing, is there a specific reason for not using the more natural format {0:0.00} in both cases? If you really mean to use a placeholder for digits then # is ok, otherwise 0 is best.

Tested solution (beware it truncates and does not round) I needed some time to understand what was actually wanted:

public static string ReturnFormat(string input, int maxLength, int decimalPrecision)
{
    if (input.Length <= maxLength)
        return input;
    Char separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
    string[] inputs = input.Split(separator);
    // NB: truncating rather than rounding
    if (inputs[1].Length > decimalPrecision)
        inputs[1] = inputs[1].Substring(0, decimalPrecision);
    int digits = (maxLength - decimalPrecision - 1);
    // NB: truncating rather than rounding, adding ~ to signalize the
    // presence of missing significant digits
    if (inputs[0].Length > digits)
        inputs[0] = inputs[0].Substring(0, digits-1) + "~";
    return inputs[0] + separator + inputs[1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文