Maybe stupid question but: How to get float out of System.Single?

What is the method to extract value from System.Single to standard float variable (defined float x = ) when using J# This sounds so trivial that I must have overlooked something when I did not find the way

I have to use System.Single when I'm communicating with certain COM controls.



Answer this question

Maybe stupid question but: How to get float out of System.Single?

  • aman anand

    This question is better suited for the J# forum. Please reserve the Base Class Library forum for questions related to the BCL itself. I'm moving the thread to the J# forum.

    In answer to your question Lucian is correct that a float and System.Single are the same type. However in J# there is not a direct conversion between Single and float like there is in C#. Therefore you'll need to use a standard typecast. Note that even values like 4.5F must be typecast to Single because the compiler won't do it automatically.

    System.Single single = (System.Single)4.5F;
    float flt = (float)single;

    The conversion is legal and will work correctly. There might be a better way (I'm not a J# expert) but this seems to be the easiest.

    Michael Taylor - 6/13/06


  • rodrigoplant

    float is just a synonym for System.Single
  • wavyknight

    Thanks TaylorMichaelL your answer worked correctly.

    Actually I tried the typecast but could not make it run earlier. Problem was that I received array of System.Single from COM component and the runtime could not convert it System.Single -array. Therefore I tried to convert invidual elements of System.Object -array that contained System.Singles into float and (naturally that did not compile. A piece of now correct code to illustrate this, note two-time typecast to make it compile:

    System.Object[] va = (System.Object[])engine.get_Bounds();

    float xx,yy,zz;

    xx = (float)((System.Single)va[0]);

    yy = (float)((System.Single)va[1]);

    zz = (float)((System.Single)va[2]);

    Slightly surprisingly, like said, following typecast would create a runtime error:

    System.Single[] vas = (System.Single[])va;


  • Yogi Verité

    you should be able to use

    Single s = new Single(4.5F);
    single flt = (single)s;

    I don't know of a "float" type in Java, don't suppose J# has such either. It's only "single" and "double" if I remember well (float is ambiguous and in languages like Fortran [and C too if I remember well] uses the native CPU precision, e.g. 32-bit [=single] on Pentium and 64-bit [=double] on x64 machines and even higher on any higher-end machine)

    also I'm not so sure

    Single s=(Single)4.5F;

    works in Java and/or J#

    Also java.lang.* types can be behaving differently than .net types with similar names. Do check the docs on each of them



  • Maybe stupid question but: How to get float out of System.Single?