Convert int to float

How do I properly convert an int to a float?
I input a number such as 0x42c26163
The final number should be 97.19021
I thought I could just do a cast so I tried this:
int test = 0x42c26163;
float test2 = (float)test;
But test2 comes up 1.120035171 E9
How do I get to the final 97.19021?
In another project I'm working on using C#, the conversion works when I use the BitConverter class.
I'm sure I am just overlooking the obvious, but can someone can help me?

I suppose that int number is really the content of a float read back from raw memory or received from an instrument or on some communication channel. Rebuilding the float number has been discussed a couple of ways in the forums: I can suggest you look at this thread or this other one which give you feasible solutions.
This is basically a reflect of how floating point numbers are stored in memory according to IEEE754 standard, which you may want to take a look at to understand a bit how all the matter is treated.
Proud to use LW/CVI from 3.1 on.
My contributions to the Developer Zone Community
If I have helped you, why not giving me a kudos?

Similar Messages

  • Can't convert int to java.lang.Integer.

    Hi everyone.
    I'm working on an auction site at the moment and have problems gaining a bidID number. This is part of my code and my error.
    AuctionFacade aHse = (AuctionFacade) application.getAttribute("AuctionFacade");
    String strTraderName = (String)session.getAttribute("traderName");
    String strPassword = (String)session.getAttribute("password");
    String strIDNum = request.getParameter("auctionID");
    Integer intID = Integer.valueOf(strIDNum);
    Integer [] bids = aHse.getAllBids(intID);
    float fltYourBid = aHse.getBidPrice(bids.length);
    ^
    Can't convert int to java.lang.Integer.
    can anyone help please?

    So, does "aHse.getBidPrice" expect an int or an Integer as its parameter? And does it return an int, or an Integer, or what? The answer to those questions may lead to your solution -- look at what you are actually using as the parameter, for example.

  • Converting Vector with Floats to float[] typecast error?

    Java Developers,
    I am able to convert my String vector to a
    String[] but just can't get the Vector with Floats to be converted to a float[] array. I have looked into google and java.sun.com Forums but still cant seem to find the answer. I would really appreciate your help!
    This is how I am making the conversion from Vector(String) to String[]:
    legendLabelsArray = new String[columnHeads.size()];
    int k=0;
    Iterator e = columnHeads.iterator();
    while(e.hasNext()){
    System.out.println("inside the enumerator");
    legendLabelsArray[k] = e.next().toString();
    System.out.println("Array elements at " + k + " are " + legendLabelsArray[k]);
    System.out.println(k++);
    How can I make something similar to work with a Vector with Floats to float[] instead of Strings?
    Thanks,
    Musaddiq
    [email protected]

    Something like this ...
    float[] floatArray = new float[vectorOfFloats.size()];
    int k=0;
    Iterator e = vectorOfFloats.iterator();
    while(e.hasNext()){
    floatArray[k] = ((Float) e.next()).floatValue();
    k++;

  • CAST Not working for me - Arithmetic overflow error converting int to data type numeric - error

    GPM is DECIMAL(5,2)
    PRICE is DECIMAL(11,4)
    COST is DECIMAL(7,2)
    Trying to update the Gross Profit Margin % field and I keep getting the "Arithmetic overflow error converting int to data type numeric" error.
    UPDATE SMEMODETAIL SET SMD_GPM = (SMD_PRICE-SMD_COST) / SMD_PRICE * 100
    FROM SMEMODETAIL WHERE SMD_PRICE<>0 AND SMD_QUANTITY<>0
    Example record:
    SMD_PRICE    SMD_COST    GPM%
    1.8500            1.62                12.4324324324324300
    I added cast and I still get the error.
    How do I format to get this to work?
    Thanks!

    Hi GBerthume,
    The error is caused by some value such as 1000.01 of the expression (SMD_PRICE-SMD_COST) / SMD_PRICE * 100 exceeds the
    precision of the column(DECIMAL(5,2)). The example data doesn't cause the overflow error for the value of the expression is 12.43 which is in the scope of DECIMAL(5,2).
    USE TestDB
    CREATE TABLE SMEMODETAIL
    SMD_PRICE DECIMAL(11,4),
    SMD_COST DECIMAL(7,2),
    SMD_GPM DECIMAL(5,2)
    INSERT INTO SMEMODETAIL(SMD_PRICE,SMD_COST) SELECT 1.8500,1.62
    UPDATE SMEMODETAIL SET SMD_GPM = (SMD_PRICE-SMD_COST) / SMD_PRICE * 100
    FROM SMEMODETAIL WHERE SMD_PRICE<>0-- AND SMD_QUANTITY<>0
    SELECT * FROM SMEMODETAIL
    DROP TABLE SMEMODETAIL
    The solution of your case can be either scale the DECIMAL(5,2) or follow the suggestion in Scott_morris-ga's to check and fix your data.
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • Converting string to float

    I have a decimal string "122339994" which i am trying to convert to float using Float.parseFloat. This results in incorrect value of 1.22339992E8.
    I thought for floats precsision comes into effect after decimal places.
    public static void main(String[] args) {
    String floatString = "122339994";
    float floatNumber = Float.parseFloat(floatString);
    System.out.println("Float is "+floatNumber+". Now double "+Double.valueOf(floatString));
    }

    See this API
    [Java2SE Float|http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29]
    Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
    "1.00000017881393421514957253748434595763683319091796875001d"
    results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.
    Its better to see the Java APIs first for any information, we will get almost all the information we need from APIs
    Regards,
    Venkatesh

  • Can't convert int to int[]    URGENT!

    HI list!
    Here is the code part I have:
    /** Holds the permutated groups which are arrays of integers. */
    Vector v;
    if(arg.length<3) System.exit (0); //Need at least 3 arguments
    elements=new int[arg.length-1]; //Create array to hold elements
    /* Copy the arguments into the element array. */
    for(i=0;i<arg.length-1;i++) elements=Integer.parseInt(arg[i+1]);
    groupsize=Integer.parseInt(arg[0]); //Get the number in each group.
    calc(groupsize,elements.length); //Find out how many permutations there are.
    v=permutate(groupsize,elements); //Do the permutation
    for(i=0;i<v.size();i++) { //Print out the result
    elements=(int[]) v.get(i);
    System.out.println("");
    for(j=0;j<elements.length;j++) System.out.print(elements
    [j]+" ");
    System.out.println("\nTotal permutations = "+v.size());
    and the error I get is:
    Permutate.java:26: Incompatible type for =. Can't convert int to int[]. for(i=0;i<arg.length-1;i++) elements=Integer.parseInt(arg[i+1]);

    elements=(int[]) v.get(i);
    The above statement is illegal. Its not possible to
    use an array type in casting at all.
    Basically you are trying to do a kind of multiple
    casting, which is not possible.
    You might want to store vectors in vector v instead of
    arrays in vector v. Its better to do this.There's nothing wrong with the line
    elements = (int[]) v.get(i);
    assuming the returned object from the vector is in fact an array of int. Even then, it is a runtime exception condition not a compile-time error.
    Using arrays is perfectly valid where you know the size of the array at creation and do not need to resize it during use... The arrays are fast and compact. This is however not a comment on the approach taken in this case but merely an observation that arrays are not always an inferior choice to one of the collection classes.
    Now put up your dukes... ;)

  • Can't convert int to java.lang.Object

    When I deploy my customer Bean, the following error occures, I think the main problem is Can't convert int to java.lang.Object, does anyone facing the same problem with me?
    <pre>C:\j2sdkee1.3\repository\home\gnrtrTMP\CustomerApp\CustomerBean_RemoteHomeImpl.j
    ava:27: Incompatible type for declaration. Can't convert int to java.lang.Object
    java.lang.Object primaryKey = ejb.ejbCreate(param0, param1, para
    m2);
    ^
    C:\j2sdkee1.3\repository\home\gnrtrTMP\CustomerApp\CustomerBean_RemoteHomeImpl.j
    ava:57: Incompatible type for =. Can't convert int to java.lang.Object.
    primaryKeys = ejb.ejbFindByPrimaryKey( param0);
    ^</pre>
    I really need your help!!!

    you can only convert one object type to another. Integer is an object int isnt. You might want to convert int to Integer and then go ahead.
    see if it works.

  • Convert string to floating-point

    Hi all,
    ..very basic question, but I tryed it for hours and only received short-dumps
    <b>How can I convert a string into a floating-point number?</b>
    Kind regards,
    Stefan

    hi
    try this
    to convert  string to float.
    data : a type f,
    s type string value '1.023'.
    a = s.
    write :/ a.
    to convert float to string.
    data : a type f value '1.023',
    s type string.
    s = a.
    write : s.

  • Can u tell me how to convert int value in to string

    hi to all, can u tell me how to convert int value in to string.

    hi to all, can u tell me how to convert int value in
    to string.Even this way:
    int number = 155;
    String mystring = ""+155;
    [\code]                                                                                                                                                                                                                                                                                               

  • Error message:can't convert int to int[]

    Hi,
    I am trying to build a java code which convert decimal to binary
    but ,I found error message "can not convert int to int[]"
    some line of code:
    int i=12,n=0;
    int arr[n]=i%2;
    n++;
    here I am trying to store remainder in an integer arrary.
    how can i do this???????
    guide me please
    -shobhit singh

    shobhit_onprob wrote:
    i want to store some integer values in to an integer arraryDid you follow the address that was given to you? I doubt it, and even if you did, 2 minutes is not enough time to read, let alone understand, the tutorial.
    Go back there, it describes all the basics you need.

  • Looking for VI where i can convert covert a float number to Q 11.5 representation

    looking for VI where i can convert covert a float number to Q 11.5 representation and what exactly  Q 11.5 representation means.
    Kindly help me on the same.
    Solved!
    Go to Solution.

    Appears to be right, but don't forget to round to the nearest integer.
    http://zone.ni.com/reference/en-XX/help/371361H-01/glang/round_to_nearest/
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Convert int to Color

    hi...does anyone know how to convert int value (grabbed from getRGB()) into the actual color format?

    hi
    use this
    Color(int rgb)
    Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
    just for
    int      getRGB()
    Returns the RGB value representing the color in the default sRGB
    regards
    Aniruddha

  • Convert short[] to float[]

    Hi, I'm trying to convert a short[] to a float[]. The following code works fine, but when I try to convert between arrays I get "cannot resolve symbol" error. Can anyone tell me the cause of this error? Thanks.
    short s=2;
    float f;
    f = new Float(s).floatValue(); //works fine
    short[] shortArray;
    float[] floatArray;
    floatArray = new Float(shortArray).floatValue(); //gets error

    Firstly don't use new AnyClass() to do conversions between primatives, it creates object unneccassaily. You can cast primative types from one value to another.
    Don't use float unless you really need to. double is just as fast and more accurate. The amount of memory you will save will be worth less than 1 cent. (most of the time)
        public static double[] convertToDoubleArray(short[] shortArray) {
            double[] ret = new double[shortArray.length];
            for (int j = 0; j < shortArray.length; j++)
                ret[j] = shortArray[j];
            return ret;
        }

  • How to determine INT and FLOAT in a DECODE statement

    Can anyone help me with the following problem:
    I have a column defined as a NUMBER(15,4). I am trying to determine the java type using the getScale() method in ResultSetMetaData. When the scale is greater than 0, then it is an float, else it is an int. In this case, the getScale() method should return 4. However, when querying with a DECODE statement on the NUMBER(15,4) field, the getScale() method always returns 0 and the value is therefore converted to an int. Anyone knows how to solve this problem?
    Thanks

    Alcides,
    Oracle NUMBER data type is mapped to "java.math.BigDecimal" class.
    You will find more information in the JDBC Developer's Guide and Reference which is part of the Oracle documentation and available from:
    http://www.oracle.com/technology/documentation/index.html
    Good Luck,
    Avi.

  • Converting Byte [] into float []

    Hi, I read the contents of a file into a byte array and am now trying to convert it into a float array.
    here is the code I am using
    public static float [] bytetofloat(byte [] convert){
        float [] data = new float [convert.length/2];
        for(int i = 0;i<convert.length;i+=2){
            for(int j = 0; j <data.length;j++){
            short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
            float valueAsFloat = (float)valueAsShort;
            System.out.println(valueAsFloat);
            valueAsFloat = data[j];
            System.out.println(data[j]);
        }can anyone see anythign wrong with the way I am doing this? I cant see anythign wrong but need to make sure its fine before I can continue.
    any advice on this or a better way to do it would be much appreciated.

    ultiron wrote:
    I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
    the way the bytes are shift they should only take up 16 bits.It's not that simple.
    First, a float in Java is always 4 bytes. The fact that it has an integer value that can fit into two bytes is irrelevant
    Second, floating point representation is not the same 2s complement that's used for byte, short, int, and long, so you're not just shifting the bits.
    For eample:
    1,  int: 00000000 00000000 00000000 00000001
    1, float: 00111111 10000000 00000000 00000000
    -1,  int: 11111111 11111111 11111111 11111111
    -1, float: 10111111 10000000 00000000 00000000Having said that, you're casting, so your step of going from short to float is correct. It doesn't just shift; it does conversions like the above. The caveats are:
    1) Is your conversion from bytes to short correct? That depends on what those bytes are supposed to be. If they're big-endian representations of 2-byte, 2s-complement numbers, then your code looks correct. You'll still have to test it of course. You'll want to focus on corner cases.
    00 00 --> 0
    00 01 --> 1
    10 00 --> 4096
    7f ff --> 32767
    80 00 --> -32768
    ff ff --> -1
    2) Can float hold all of short's values? I think it can. It obviously can't hold all of int's values, but I think it's precision is sufficient to cover short.
    By the way, is there a reason you're using float instead of double? Unless you're in an extremely memory-constrained environment, there's no reason to use double.

Maybe you are looking for

  • 5th gen iPod not playing mpeg4 videos

    I have a few mpeg4 videos that I wanted to add to my iPod. They play in iTunes and iTunes recognizes them as the correct format for my iPod. So, I connect my iPod and the videos uploaded themselves with the auto-update. After the update, I grabbed th

  • Netui:textBox tag attributes - readonly and disabled

              What's the difference between these two?           Thanks           

  • Ihave firfoe 3.6 and I have Adobe Flash but can,t play videos.

    I have windows 7 on a new HP laptop and have downloaded Firefox , Adobe Flash and Java and cant see any type of videos. Ever time I go to view a video I get the message to download Flash and Java and on video can you help me

  • LKM for MySQL Error

    Hi, Can someone help me please. I am new to ODI and trying to work with it. My source DB = MS SQL 2005 Server Target DB = MySQL5.1 I created a new project and models. I have the physical and logical architecture and all the connections are working. I

  • XMLSocket onClose event is unreliable

    I use XMLSocket in my application and I have connection management code that automatically reconnects if onClose is called. However, I've found that often (especially if the connection is open for a long time) the onClose event isn't called. What cau