[Python.NET] Conversion of single precision floating point to python floats

Peter Schwalm Peter.Schwalm at epost.de
Sun Feb 6 01:58:27 CET 2005

Hi Brian,

I recently had a problem retrieving a float value from a dotNet function. I
had a conversion error which was caused by the local setting of the decimal
separator (a comma here in germany).

I figured out that you have already corrected this problem in cvs. Before I
found these correction I have tried the following version, which I would
prefer because it does not use an intermediate string for the conversion.

I don't know if you use this intermediate string accidentally - possibly for
the sake of rounding that happens during conversion to a string?

Anyway I would like to communicate my solution (your last cvs version
commented out):

namespace Python.Runtime {
    internal class Converter {
    internal static IntPtr ToPython(Object value, Type type) {

    case TypeCode.Single:
//      string ss = ((float)value).ToString(nfi);
//      IntPtr ps = Runtime.PyString_FromString(ss);
//      IntPtr op = Runtime.PyFloat_FromString(ps, IntPtr.Zero);
//      Runtime.Decref(ps);
//      return op;
        return Runtime.PyFloat_FromDouble((double) ((float)value));

Peter Schwalm

More information about the PythonDotNet mailing list