Converting floats to Strings and back

Discussion in 'Java' started by dcMan, Nov 21, 2005.

  1. dcMan

    dcMan Guest

    Hello,
    I'm trying to convert a String to a float, do some arithmetic on it, then
    display the new value on a Swing text field. Then do the reverse. Take a
    decimal numeric value the operator has entered from the GUI, convert it to a
    float do some arithmetic to remove the decimal, convert it back to a String
    to save it to a database.

    The code works fine until I exceed 8 digits. Beyond 8 digits the float is
    stored in memory in scientific notation (found this out by stepping through
    using the debugger) so when it is converted to a String I do not get the
    correct data. I get something like 1.234567891E7 instead of 12345678.91

    I've also tried using a double but I got the same results.

    String sWeight = "";
    float floatWeight= 0;

    //Retrieve weight from database stored as a String values, with no decimals
    sWeight = getWeight();
    // need to put a two digit decimal place within the number before
    displaying, so divide by 100
    floatWeight = Float.parseFloat(sWeight) / 100.00;
    // convert the float to a string and display on the GUI.
    gui_text_field.setText(String.valueOf(floatWeight);


    I also need to the same thing in reverse, if the operator manually enters
    the weight it must be save back to the database as a String.

    String myString = "";
    float floatValue = 0;

    //Get the text string entered by operator (ex 12345678.91)
    myString = gui_text_field.getText();
    // move the decimal to the end of the number, so multiply by 100
    floatValue = Float.parseFloat(myString) * 100;

    // convert the weight to a string
    myString = Float.toString((float)floatValue);
    // strip out the decimal point
    myString = myString.substring(0, myString.indexOf("."));

    // save String to the String data column in database, myString should be
    equal to 1234567891
    saveDataToDatabase(myString);

    Thanks in Advance
     
    dcMan, Nov 21, 2005
    #1
    1. Advertising

  2. dcMan

    Roedy Green Guest

    On Mon, 21 Nov 2005 17:26:07 GMT, "dcMan" <>
    wrote, quoted or indirectly quoted someone who said :

    >I'm trying to convert a String to a float, do some arithmetic on it, then
    >display the new value on a Swing text field. Then do the reverse. Take a
    >decimal numeric value the operator has entered from the GUI, convert it to a
    >float do some arithmetic to remove the decimal, convert it back to a String
    >to save it to a database.


    see http://mindprod.com/applets/converter.html

    You want interconvert float <-> String.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 21, 2005
    #2
    1. Advertising

  3. dcMan

    Guest

    Use the Big Decimal class ;)
     
    , Nov 21, 2005
    #3
  4. dcMan

    Oliver Wong Guest

    "dcMan" <> wrote in message
    news:pqngf.2966$...
    > Hello,
    > I'm trying to convert a String to a float, do some arithmetic on it, then
    > display the new value on a Swing text field. Then do the reverse. Take a
    > decimal numeric value the operator has entered from the GUI, convert it to
    > a float do some arithmetic to remove the decimal, convert it back to a
    > String to save it to a database.
    >
    > The code works fine until I exceed 8 digits. Beyond 8 digits the float is
    > stored in memory in scientific notation (found this out by stepping
    > through using the debugger) so when it is converted to a String I do not
    > get the correct data. I get something like 1.234567891E7 instead of
    > 12345678.91


    The "in memory representation" of a float is probably not in scientific
    notation; I'm not sure if the Java Language Specification actually forces
    this, but every implementation I've seen stores uses IEEE floating point
    format. When you try to get a string representation of the value, that
    string may be displayed in scientific notation. To avoid that, you may wish
    to use the java.text.DecimalFormat class.

    http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

    - Oliver
     
    Oliver Wong, Nov 21, 2005
    #4
  5. dcMan

    Alun Harford Guest

    "dcMan" <> wrote in message
    news:pqngf.2966$...
    > Hello,
    > I'm trying to convert a String to a float, do some arithmetic on it, then
    > display the new value on a Swing text field. Then do the reverse. Take a
    > decimal numeric value the operator has entered from the GUI, convert it to

    a
    > float do some arithmetic to remove the decimal, convert it back to a

    String
    > to save it to a database.
    >
    > The code works fine until I exceed 8 digits. Beyond 8 digits the float is
    > stored in memory in scientific notation (found this out by stepping

    through
    > using the debugger) so when it is converted to a String I do not get the
    > correct data. I get something like 1.234567891E7 instead of 12345678.91


    Unless you want speed (ie. the arithmetic is hard and you've analysed the
    result of using floating-point to do it), or you want to demonstrate some of
    the nasty things that happen with floating point, use BigDecimal.
    a) It requires significantly less use of your brain, which generally reduces
    the number of bugs.
    b) Your users shouldn't have to think about the limitations of floating
    point without a very good reason.

    Alun Harford
     
    Alun Harford, Nov 22, 2005
    #5
  6. dcMan

    dcMan Guest

    Thanks, the Big Decimal class worked great.


    <> wrote in message
    news:...
    > Use the Big Decimal class ;)
    >
     
    dcMan, Nov 22, 2005
    #6
  7. dcMan

    Oliver Wong Guest

    Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back]

    "Alun Harford" <> wrote in message
    news:dlvmk8$j8e$...
    >
    > Unless you want speed (ie. the arithmetic is hard and you've analysed the
    > result of using floating-point to do it), or you want to demonstrate some
    > of
    > the nasty things that happen with floating point, use BigDecimal.
    > a) It requires significantly less use of your brain, which generally
    > reduces
    > the number of bugs.
    > b) Your users shouldn't have to think about the limitations of floating
    > point without a very good reason.


    I've experimented with using BigInteger instead of int in my code with
    mixed results. Code that was integer-math intensive (e.g. finding prime
    numbers) typically ran 5 to 7 times slower, which is pretty bad, but not
    "noticeable" for typical user applications. I'll probably continue this
    practice because for most of my apps, the bottleneck is not the
    integer-math, and the extra flexibility is nice.

    My question is: is there a "best" way to store BigInteger and BigDecimal
    values in databases (particularly in SQL)? The two most obvious solutions to
    me is to store them as BLOBs or as strings. I'd probably favor the latter,
    because although it uses more storage space and processing time (e.g. to
    parse the string back into a BigInteger value), it'll probably be easier to
    inspect the DB to make sure all the values are correct during debugging.

    - Oliver
     
    Oliver Wong, Nov 22, 2005
    #7
  8. dcMan

    Chris Uppal Guest

    Re: Using BigInteger and BigDecimal [WAS: Converting floats to Strings and back]

    Oliver Wong wrote:

    > My question is: is there a "best" way to store BigInteger and
    > BigDecimal values in databases (particularly in SQL)? The two most
    > obvious solutions to me is to store them as BLOBs or as strings. I'd
    > probably favor the latter, because [...]


    Also, other applications that read the data (not Java) will be able to work
    with it. E.g. report generators.

    Since one of the biggest (arguably the only) reason to keep data in SQL-style
    databases is so that the data can be application-independent, it makes sense to
    keep data in application-independent formats where possible.

    -- chris
     
    Chris Uppal, Nov 23, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,344
    Tim Rentsch
    Sep 23, 2005
  2. Replies:
    6
    Views:
    289
    Dan Bishop
    Apr 6, 2008
  3. Daniel Fetchinson
    Replies:
    13
    Views:
    1,111
    Daniel Fetchinson
    Dec 28, 2008
  4. Dirk T. Shelley

    Converting Floats to Strings yields erratic results

    Dirk T. Shelley, Jun 7, 2011, in forum: C Programming
    Replies:
    29
    Views:
    817
    Seebs
    Jun 10, 2011
  5. Dave Burt
    Replies:
    0
    Views:
    117
    Dave Burt
    May 7, 2005
Loading...

Share This Page