【问题标题】:C# : Check value stored inside string object is decimal or notC#:检查存储在字符串对象中的值是否为十进制
【发布时间】:2009-11-13 17:45:16
【问题描述】:

在 C# 中,我如何检查存储在字符串对象中的值(例如:字符串 strOrderId="435242A")是否为十进制?

【问题讨论】:

    标签: c# decimal


    【解决方案1】:

    使用Decimal.TryParse 函数。

    decimal value;
    if(Decimal.TryParse(strOrderId, out value))
      // It's a decimal
    else
      // No it's not.
    

    【讨论】:

    • 这只有在任何数字都可以被视为十进制时才有效。如果您需要区分数字类型,它也会将整数类型视为小数。
    • 您应该考虑十进制格式和当前文化。例如,en-Us 643.57 的正确十进制值不会通过此方法解析 ru-RU 区域性。
    • 我知道这是旧的,但我添加了一些额外的验证并检查了十进制逗号,在我的例子中是小数分隔符。 if (!Decimal.TryParse(strOrderId, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out decimalNumber) || strOrderId.IndexOf(",") > -1) { // not Decimal } else { // decimal }
    【解决方案2】:

    您可以使用Decimal.TryParse 来检查该值是否可以转换为Decimal 类型。如果将结果分配给 Double 类型的变量,也可以使用 Double.TryParse。

    MSDN 示例:

    string value = "1,643.57";
    decimal number;
    if (Decimal.TryParse(value, out number))
       Console.WriteLine(number);
    else
       Console.WriteLine("Unable to parse '{0}'.", value);
    

    【讨论】:

      【解决方案3】:
      decimal decValue;
      
      if (decimal.TryParse(strOrderID, out decValue)
      { / *this is a decimal */ }
      else
      { /* not a decimal */}
      

      【讨论】:

        【解决方案4】:

        你可以尝试解析它:

        string value = "123";
        decimal result;
        if (decimal.TryParse(value, out result))
        {
            // the value was decimal
            Console.WriteLine(result);
        }
        

        【讨论】:

          【解决方案5】:

          如果我们不想使用额外的变量。

          string strOrderId = "435242A";
          
          bool isDecimal = isDecimal(strOrderId);
          
          
          public bool isDecimal(string value) {
          
            try {
              Decimal.Parse(value);
              return true;
            } catch {
              return false;
            }
          }
          

          【讨论】:

            【解决方案6】:

            利用丢弃符号_并使用

            if (decimal.TryParse(stringValue, out _))
            {
               // valid decimal 
            }
            else
            {
               // not decimal
            }
            

            【讨论】:

              【解决方案7】:

              这个简单的代码将允许整数或十进制值,并拒绝字母和符号。

                    foreach (char ch in strOrderId)
                      {
                          if (!char.IsDigit(ch) && ch != '.')
                          {
              
                            MessageBox.Show("This is not a decimal \n");
                            return;
                          }
                         else
                         {
                         //this is a decimal value
                         }
              
                      }
              

              【讨论】:

                【解决方案8】:

                在 TryParse 中声明小数输出值

                if(Decimal.TryParse(stringValue,out decimal dec))
                {
                    // ....
                }
                

                【讨论】:

                  【解决方案9】:

                  想得简单。

                  decimal decNumber = decimal.Parse("9.99");
                  if (decNumber % 1 > 0)
                  {
                     //decimal area
                  }
                  else
                  {
                     //int area
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-07-20
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-04-02
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-01-13
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多