Converting double to float

This problem only occurs when dealing with large numbers or numbers with a lot of fraction digits.
Here is an example:
double d = 7.1E9;
float f1 = (float)d;
float f2 = new Float(d).floatValue();
System.out.println( d + " " + f1 + " " + f2);
The result is:
7.1E9 7.1000003E9 7.1000003E9
I tried other ways of converting to float (using DecimalFormat) but unfortunately nothing works. The resulting float is not the same as the original double.
Any ideas?
Thanks.
Igor.

float is only 32 bit while double is 64 bit, so you can easily get differences like that.
Is it a problem? If you results gets inaccurate you should stay with double or use BigDecimal if you really care about the precision. If it's because you want to round the result of before you display it, you can use DecimalFormat.

Similar Messages

  • Convert double precision float string to a number

    I am a newbie with Labview.
    I have a GPIB link with a vector voltmeter and I receive the data as a double precision float string (IEEE standard 754-1985). I would like it to be recognized as a number, but I can't find the proper conversion function. The string to convert is 8 byte long. Does anyone have an idea how to do it? Thank you!

    Asumming your data is big-endian, you should be able to simply typecast it to a DBL (see attache LV 7.0 example).
    (It is possible that your DBL string is little-endian, in this case it will be slightly more complicated. Let us know )Message Edited by altenbach on 05-27-2005 04:49 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    StringToDBL.vi ‏23 KB

  • Help with double and float

    I added the (float) after the = in both and it fixed the top and not the bottom. "cant convert double to float" (as well as the answer to this, if somone could correct my terminology as well it would be appretiated. thanks in advance)
    float torontoF = (float)(torontoC * 1.8) + 32;
         System.out.print (torontoF);
    x      float stockholmC = (float)(stockholmF ? 32)* .5556;
         System.out.print (stockholmF);

    Reposted and answered at:
    http://forum.java.sun.com/thread.jspa?threadID=5219089
    Do not reply here.

  • How to convert a String data to Double or Float???? Thank you

    I need to convert String data to Double or Float , please help me about it. Thank you very muh!
    String a=Integer.parseInt("1234"); convert to integer.

    I found it but do not know if it is ok
    float a= Float.valueof(String to
    convert).floatValue();
    may be it can be!!!
    Thank you !!!!How did parseInt lead you do valueOf?
    Look for something more consistent. A pattern.As I said earlier, it depends if he wants an object or a primitive.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Cannot explicit convert double.MaxValue/double.MinValue/float.MaxValue/float.MinValue to decimal

    I'm try set double.MaxValue/double.MinValue/float.MaxValue/float.MinValue to NumericUpDown's Maximun/Minimum
    It fail (compile error)
    but variable and constant's explicit conversion is OK.
    can anyone tell me why?
    the screenshot is in below
    http://i.imgur.com/woZXr0i.png

    yes, your right!
    I has been tried
    If I assign it(MinValue/MaxValue) to a variable first and then explicit convert to a decimal
    it will be pass compile, but it will throw exception when runtime
    screenshot
    http://i.imgur.com/TpD8guJ.png

  • Casting arrays from double to float

    Hi, wonder if anyone can help.
    I have a method of a class that creates two double arrays (the value pointed to is a double type)
    x[][]
    y[][]
    they are essentially co-ordinates. The first dimension of the array holds the polygon number, the second the point on that polygon for the x and y respectivly.
    This method is called from (and therefore the arrays returned to) a class in an applet i am designing.
    I now have to get these co-ords into a general path for display. A general path only takes float values. I have tried all the normal casting tricks to try to convert these doubles into floats and nothing is working. Do you have to do anything special because they are in arrays.
    Any help would be appriciated.

    There is nothing special about array except that you cannot cast them.
    Instead you need to create a new set of arrays and copy/cast each value individually.

  • 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

  • Converting double to String

    I am looking for a way to convert double to String.
    I think that I have to use wrapper like Double class and use toString(double d), but I am not sure how to do it.
    Since toString() is static method, I do not have to have an instance of the Double class, so I was wondering what's the correct way to do it.
    I need something like this:
    String MyString = new String (String1.concat ( My_doubleNumber.Double.toString() ) );
    Any Suggestions?
    Thanks!!!

    WOW thanks a lot. These are definately better than what I thought of doing originally.
    In case I need to use the wrapper class somewhere else, I am just wondering what would be a proper use of Double and String?

  • Converting Double to String without exponent

    Hi all,
    I am reading some values from Excelsheet using apache POI library. I have large numbers that need to stored into database as string and not numbers. hence, while reading the excelsheet values, i convert these number.. and the are received as double. While converting double into string I noticed that some values are represented in exponential form.. I would like to avoid this and have pure number values in the form of string,,..
    Can anybody tell me how to do it?
    Thanks in advance,
    Abdel Olakara

    java.text.DecimalFormat

  • 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.

  • FM to convert double byte chars

    Hi All,
    Is there anyone know what are the function module to convert double-byte chars? Thanks.

    Seems like Blue Sky is not clear
    You want to convert what into what?
    Whats the purpose of this requirement, kindly give more details.
    Regards
    Karthik D

  • Convert Double to double

    How can I convert Double to double???

    It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.
    Java� API Specifications
    Java� 1.5 JDK Javadocs

  • 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

  • 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++;

  • Double to float

    I found in APIs, there is a method: floatValue() to transfer double to float.But when I wrote the following code and compiled it, the error is:
    myjava.java:437: double cannot be dereferenced
    float ba=aa.floatValue();
    program:
    double aa=4;
    float ba=aa.floatValue();
    What's the problem in my program?
    Thanks

    That does not work because the floatValue() method exists in the Double wrapper class and not for the double primitive type. You should try either:
    double aa = 4;
    Double d = new Double(aa);
    float ba = d.floatValue();or
    double aa = 4;
    float ba = (float)aa;

Maybe you are looking for

  • How do I duplicate the Julieanne Kost watermark procedure

    First, I must explain I am a beginner and have been having difficulty executing a successful watermark procedure, specifically making original line drawing art a watermark. I recently ordered Photoshop For Dummies online. The best help I have found i

  • How TO download itunes from Imac G5 to New Ipad

    After receiving the new Ipad for xmas this year year I was overjoyed with excitment! It took me back to being a little girl again. So I tried to set up my Itunes on the Ipad, and to my annoyment my Imac G5 using only itunes 10 could not download the

  • How to add BookMarks in Portal release 2

    Hi, I want to know whether it is possible to add bookmarks in oracle Portal if yes how. thanks, Pravin

  • Can't download in message+

    When I receive messages in message+ there is an icon that reads download,  which when selected reads downloading then changes back to download without content showing. I checked my downloads on my s5 and it is empty. How do I view my messages,  I rec

  • Is there any BAPI available for sales order creation with reference?

    Hi Experts, My requirement is to create sales order with reference for order type. Now my billing document number already contains the two items one is correct and another is zero quantity. I want to create order with reference of billing document bu