not able to get float value from python script in C# windows app
Question: I have a python script which reads datatable passed from C#. But when i try to
get the value of variable in python script.. it does not fetch value. This
issue occurs only for varible of type float. Example when i try to fetch value
of "ErrorRate" ( in python script), it returns 0.0 value only.. though the
actaule value is 37.55

// C# script

    
    
    // dt is datatable from C# being passed to python
    var engine = Python.CreateEngine();
                   ScriptScope scope = engine.CreateScope();
    
                   ScriptRuntime runTime = Python.CreateRuntime();
                   ScriptEngine pyEngine = runTime.GetEngine("py");
                   MemoryStream ms = new MemoryStream();
    
                   scope.SetVariable("dt",dtmp);
    
                   runTime.IO.SetOutput(ms, new StreamWriter(ms));
                   ScriptSource ss = pyEngine.CreateScriptSourceFromString(
                                         txtPythonCode.Text.Trim(),
                                          SourceCodeKind.Statements);
    
                   ss.Execute(scope);
                   dynamic v1 = scope.GetVariable("Error");
                   dynamic v3 = scope.GetVariable<float>("ErrorRate");
                   string str = ReadFromStream(ms);
    
    // Python script
    
        import clr
        clr.AddReference('System.Data')
        from System import Data
        from System.Data import DataTable
    
        TW = "Button1"
        TotalCnt =0;
        ErrorRate = 0.0;
        Error =0;
    
    
        # Function definition is here
        def GetErrorRate(erate,total):
           ErrorRate = float( erate / TotalCnt )*100
           print("Error rate - ",float( erate / TotalCnt )*100);
           return;
    
        for row in dt.Rows:
            if TW == row[3]:
               print("TotalCnt  - ",TotalCnt)     
            elif row[3]=="X":
               TotalCnt = TotalCnt +1
            elif row[3]=="Y":
               TotalCnt = TotalCnt +1
            elif row[3]=="Z":
               TotalCnt = TotalCnt +1
            elif row[3]==" Wire":
               print()
            else:
              TotalCnt = TotalCnt +1
              Error = Error +1
    
        GetErrorRate(Error,TotalCnt)
    

Really don't know what could be the reason for the issue.


Answer: As [PM 2Ring](http://stackoverflow.com/users/4014959/pm-2ring) observed, you
are probably doing integer division. This means any decimal part of number
gets discarded. Now the result as you told us should be `37.55`. To get that
you are multiplying by `100`, so the result of `erate / TotalCnt` should be
`0.3755`. If this would be an integer division, the result of that would be,
however, `0`. You then convert this `0` into `0.0` with `float` constructor,
multiply by `100` and there you get the result, `0.0`.

Solution for this is very simple, just use `TotalCnt = 0.0` instead of
`TotalCnt =0;`. That will make the `TotalCnt` variable `float` from start and
you will perform floating point operations each time using it.

**PS:** You don't need to use semicolons `;` after statements in python

