How to evaluate a math expression contained in a string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Juan

    How to evaluate a math expression contained in a string?




    If I have a string with a valid math expression like " (2 + 28)/1", how can
    I evaluate it?



    Thanks,

    Juan.



  • Amiram Korach

    #2
    RE: How to evaluate a math expression contained in a string?

    You can always work hard and write it yourself, but I think you are not the
    first to meet this. I found something in CodeProject:

    I you search yourself, I'm sure you'll find more.

    "Juan" wrote:
    [color=blue]
    >
    >
    >
    > If I have a string with a valid math expression like " (2 + 28)/1", how can
    > I evaluate it?
    >
    >
    >
    > Thanks,
    >
    > Juan.
    >
    >
    >
    >[/color]

    Comment

    • James Curran

      #3
      Re: How to evaluate a math expression contained in a string?

      Try the .Math library:
      http://hebertsoft.com/dotmath/ (Note, he's moving the downloads from
      http://workspaces.gotdotnet.com/math to
      https://sourceforge.net/projects/dotmath/, but is still in the process)



      "Juan" <juan_mauricio_ lopez@ANTISPAMh otmail.com> wrote in message
      news:uSraggvBFH A.1296@TK2MSFTN GP10.phx.gbl...[color=blue]
      >
      >
      >
      > If I have a string with a valid math expression like " (2 + 28)/1", how[/color]
      can[color=blue]
      > I evaluate it?[/color]


      Comment

      • Jako Menkveld

        #4
        Re: How to evaluate a math expression contained in a string?

        I'm not sure how safe this is, but I used this in a non-production
        environment and it works quite well:


        "Amiram Korach" <AmiramKorach@d iscussions.micr osoft.com> wrote in message
        news:64F416D0-9314-47F8-B84D-3A2BFF0484B9@mi crosoft.com...[color=blue]
        > You can always work hard and write it yourself, but I think you are not
        > the
        > first to meet this. I found something in CodeProject:
        > http://www.codeproject.com/vb/net/ma..._evaluator.asp
        > I you search yourself, I'm sure you'll find more.
        >
        > "Juan" wrote:
        >[color=green]
        >>
        >>
        >>
        >> If I have a string with a valid math expression like " (2 + 28)/1", how
        >> can
        >> I evaluate it?
        >>
        >>
        >>
        >> Thanks,
        >>
        >> Juan.
        >>
        >>
        >>
        >>[/color][/color]


        Comment

        • Jako Menkveld

          #5
          Re: How to evaluate a math expression contained in a string?

          Sorry about that, here's the code:

          ------------------------------------------------------------------------------
          using System;

          using Microsoft.JScri pt;
          using Microsoft.JScri pt.Vsa;

          namespace VPDBUtils
          {
          public class JScriptEvaluato r
          {
          private static VsaEngine vsaEngine;

          public static void Initialize()
          {
          vsaEngine = VsaEngine.Creat eEngine();
          }

          public static void Close()
          {
          if (vsaEngine != null)
          {
          vsaEngine.Close ();
          }
          }

          private static object EvaluateString( string sStringToEvalua te)
          {
          return Eval.JScriptEva luate(sStringTo Evaluate, vsaEngine);
          }

          internal static double EvaluateStringA sDouble(string sStringToEvalua te)
          {
          return System.Convert. ToDouble(Evalua teString(sStrin gToEvaluate));
          }
          }
          }
          ------------------------------------------------------------------------------


          First you call the static Initialise, the pass your string to
          EvaluateStringA sDouble() and it gives you the result back as a double.


          "Amiram Korach" <AmiramKorach@d iscussions.micr osoft.com> wrote in message
          news:64F416D0-9314-47F8-B84D-3A2BFF0484B9@mi crosoft.com...[color=blue]
          > You can always work hard and write it yourself, but I think you are not
          > the
          > first to meet this. I found something in CodeProject:
          > http://www.codeproject.com/vb/net/ma..._evaluator.asp
          > I you search yourself, I'm sure you'll find more.
          >
          > "Juan" wrote:
          >[color=green]
          >>
          >>
          >>
          >> If I have a string with a valid math expression like " (2 + 28)/1", how
          >> can
          >> I evaluate it?
          >>
          >>
          >>
          >> Thanks,
          >>
          >> Juan.
          >>
          >>
          >>
          >>[/color][/color]


          Comment

          Working...