Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.  
C#
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";
          }
      }
Posted

1 solution

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.

C#
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";
    }
}
 
Share this answer
 
Comments
jeevakumar.T 4-Sep-14 1:11am    
Thanks Friend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900