【问题标题】:STR( ) Function VFP C# equivalentSTR( ) 函数 VFP C# 等效
【发布时间】:2017-09-15 08:48:40
【问题描述】:

C# 中是否有一个等效的函数,其工作方式与 vfp 中的 STR() https://msdn.microsoft.com/en-us/library/texae2db(v=vs.80).aspx

? str(111.666666,3,3) --> 112

? str(111.666666,2,3) --> ** 错误

? str(11.666666,2,3) --> 12

? str(0.666666,4,3) --> .667

? str(0.666666,8,3) --> 0.667(即左起3个空格加上结果)

【问题讨论】:

标签: c# visual-foxpro


【解决方案1】:

如 cmets 中所述,您可以使用 .ToString() 将数字转换为字符串。您可以在 ToString 中使用标准格式或自定义格式。例如 ToString("C") 根据您的语言环境设置为您提供一个类似于 $123.46 或 €123.46 的字符串,其值为“123.46”。

或者您可以使用自定义格式,例如“0:#.##”。您可以为不同的长度或小数位使用自定义格式。 2 位小数 "0:#.##" 或 3 位小数 "0:#.###"。

有关详细说明,您可以查看文档。

标准数字格式字符串:https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

自定义数字格式字符串:https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

STR 的自定义方法

在link 的帮助下,我编写了一个快速示例。它适用于您的输入,但我没有完全测试。

public static string STR(double d, int totalLen, int decimalPlaces)
{
    int floor = (int) Math.Floor(d);
    int length = floor.ToString().Length;
    if (length > totalLen)
        throw new NotImplementedException();
    if (totalLen - length < decimalPlaces)
        decimalPlaces =  totalLen - length;
    if (decimalPlaces < 0)
        decimalPlaces = 0;
    string str = Math.Round(d, decimalPlaces).ToString();
    if (str.StartsWith("0") && str.Length > 1 && totalLen - decimalPlaces - 1 <= 0)
        str = str.Remove(0,1);

    return str.Substring(0, str.Length >= totalLen ? totalLen : str.Length);
}

【讨论】:

  • 我认为字符串并不简单。
  • 它会根据传递的值做各种事情,例如,如果最大长度小于小数位,它会将数字四舍五入,如果最大长度大于结果,它会填充空格跨度>
  • 你能举一些值作为 ToString 不够用的例子吗?
  • 我已经用一些例子更新了这个问题,谢谢
  • 我相信您应该在自定义函数中执行此操作。使用地板长度和小数位数,您可以计算格式。或者您可以从结果字符串中获取子字符串。也请检查这个问题。 stackoverflow.com/questions/20403497/…
【解决方案2】:
public static string STR(object value, long totalLen = 0, long decimals = 0) {
  string result = string.Empty;
  try {

    if (value is string) {
      return (string)value;
    }

    var originalDecimals = decimals;
    int currentLen = (int)totalLen + 1;

    while (currentLen > totalLen) {
      string formatString = "{0:N";
      formatString += decimals.ToString();
      formatString += "}";

      result = string.Format(formatString, value);

      if (result.StartsWith("0") && result.Length > 1 && totalLen - decimals <= 1) {
        // STR(0.5, 3, 2) --> ".50"
        result = result.Remove(0, 1);
      }
      else if (result.StartsWith("-0") && result.Length > 2 && totalLen - decimals <= 2) {
        // STR(-0.5, 3, 2) --> "-.5"
        result = result.Remove(1, 1);
      }
      if (totalLen > 0&& result.Length < totalLen && (decimals == originalDecimals || decimals == 0)) {
        // STR(20, 3, 2) --> " 20"
        result = result.PadLeft((int)totalLen);
      }

      currentLen = result.Length;
      if (currentLen > totalLen) {
        decimals--;
        if (decimals < 0) {
          result = string.Empty.PadRight((int)totalLen, '*');
          break;
        }
      }
    }

    return result;
  }
  catch {
    result = "***";
  }

  return result;
}

【讨论】:

  • 一些例子: STR(0.5, 3, 2) --> ".50" STR(-0.5, 3, 2) --> "-.5" STR(20, 3, 2) --> "20" STR(20, 4, 2) --> "20.0" STR(20, 5, 2) --> "20.00" STR(20, 7, 2) --> " 20.00" STR(78900, 10, 2) --> " 78,900.00" STR(99.99, 2, 0) --> "**" 使用格式提供程序获取带有适当分隔符的格式:result = string.Format(formatString, value); --> result = string.Format(&lt;IFormatProvider&gt;, formatString, value);
猜你喜欢
  • 2015-10-04
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2021-07-19
  • 2011-08-14
相关资源
最近更新 更多