Results 1 to 3 of 3

Thread: [2.0] Changing from int to double?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Unhappy [2.0] Changing from int to double?

    Well.. I am basically doing math functions and I got this from my good ol' friend, Coniptoer

    Code:
            /// <summary>
            /// A simple function to get the result of a C# expression (basic and advanced math possible)
            /// </summary>
            /// <param name="command">String value containing an expression that can evaluate to a double.</param>
            /// <returns>a Double value after evaluating the command string.</returns>
            private double ProcessCommand(string command)
            {
                //Create a C# Code Provider
                CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
                // Build the parameters for source compilation.
                CompilerParameters cp = new CompilerParameters();
                cp.GenerateExecutable = false;//No need to make an EXE file here.
                cp.GenerateInMemory = true;   //But we do need one in memory.
                cp.OutputAssembly = "TempModule"; //This is not necessary, however, if used repeatedly, causes the CLR to not need to 
                //load a new assembly each time the function is run.
                //The below string is basically the shell of a C# program, that does nothing, but contains an
                //Evaluate() method for our purposes.  I realize this leaves the app open to injection attacks, 
                //But this is a simple demonstration.
                string TempModuleSource = "namespace ns{" +
                                          "using System;" +
                                          "class class1{" +
                                          "public static double Evaluate(){return " + command + ";}}} ";  //Our actual Expression evaluator
    
                CompilerResults cr = myCodeProvider.CompileAssemblyFromSource(cp, TempModuleSource);
                if (cr.Errors.Count > 0)
                {
                    //If a compiler error is generated, we will throw an exception because 
                    //the syntax was wrong - again, this is left up to the implementer to verify syntax before
                    //calling the function.  The calling code could trap this in a try loop, and notify a user 
                    //the command was not understood, for example.
                    throw new ArgumentException("Expression cannot be evaluated, please use a valid C# expression");
                }
                else
                {
                    MethodInfo Methinfo = cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate");
                    return (double)Methinfo.Invoke(null, null);
                }
            }
    However, it appears that when dividing, it is using integers. If I put 26 / 3 in my program instead of getting 8.67, I get 8. Let's do 25 / 3. We get 8.3333 etc.. However, in my program I am still getting 8 in both cases. Anyone know how to fix this? I want decimals

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2.0] Changing from int to double?

    Since that is essentially just passing a string to the C# program contained in the TempModuleSource, you would have to do the same thing that any C# program would do when working with literals. That is, 25.0 / 8.0 will give you your decimal result. the only way around this and to always assume a decimal literal would probably be to parse the string before sending it and add the .0 when necessary. Easier said than done, however..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Changing from int to double?

    OT:

    Hey Conipto, why aren't you on aim or msn?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width