如何解决此问题输入字符串不是正确的格式。 [英] How Can I Solve This Problem Input String Was Not In A Correct Format.

查看:121
本文介绍了如何解决此问题输入字符串不是正确的格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I create one calculation project on windows form application. i created calculation formula based function. it was working good but I have one Error on Run time. If I Type Decimal Values on example(.15). i got an error  Input string was not in a correct format. i pass two input from TAPFPMcal function.
below i give my coding please help me.  

public void TAPFPMcal(string FPR, string RPM)
      {


          if (RPM == "")
          {
              DRPM = 0;
          }
          else
          {
              DRPM = Convert.ToDecimal(RPM.ToString());
          }

          if (FPR == "")
          {
              DFPM = 0;
          }
          else
          {
              DFPM = Convert.ToDecimal(FPR.ToString());
          }
          if (DFPM != 0)
          {
              Decimal FEED = 0;
              FEED = (DRPM * DFPM);
              //fpm=I27*G29
              FPM = Math.Round(FEED, 2).ToString();
          }
          else
          {
              FPM = "0.0000";
          }
      }

推荐答案

为了避免在传入无效字符串时出错,使用 decimal.TryParse 方法 [ ^ ]。



关于为什么在传入时出现错误的原因。15,检查您的区域设置。小数分隔符可能不是字符。



另外,因为你的参数已经是字符串,没有必要在它们上面调用 .ToString()



To avoid the error when an invalid string is passed in, use the decimal.TryParse method[^].

As to why you're getting an error when you pass in ".15", check your regional settings. It's possible that the decimal separator is not the "." character.

Also, since your parameters are already strings, there's no need to call .ToString() on them.

public void TAPFPMcal(string FPR, string RPM)
{
    if (!decimal.TryParse(RPM, out DRPM))
    {
        DRPM = 0;
    }
    if (!decimal.TryParse(FPR, out DFPM))
    {
        DFPM = 0;
    }
    
    if (DFPM != 0)
    {
        decimal FEED = DRPM * DFPM;
        FPM = Math.Round(FEED, 2).ToString();
    }
    else
    {
        FPM = "0.0000";
    }
}


这篇关于如何解决此问题输入字符串不是正确的格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆