14 Replies Latest reply: Aug 16, 2008 9:06 PM by 3004 RSS

    chANGING FLOAT INTO DOUBLE

    843785
      I AM TRYING TO to change numerical output to show two decimal places when I do a print out but it will not let me I have tried to change it to double but it states that it loses precision in my ide and does not work how can i do it.

      so far i have tried.

      result1 = (double) result;

      jTextArea1.setText(String.valueOf(double) result1;

      i have tried many other methods but this is the main one in the text books.

      it keeps telling me that it cannot cast it to double no matter what i do.

      any suggestions.
        • 1. Re: chANGING FLOAT INTO DOUBLE
          3004
          chANGING FLOAT INTO DOUBLE
          float f =1.23;
          double d = f;
          I AM TRYING TO to change numerical output to show two decimal places
          That has nothing to do with "chANGING FLOAT INTO DOUBLE", but check out DecimalFormat or [String.format|http://java.sun.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)]/[PrintStream.printf|http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.lang.String, java.lang.Object...)] and the [Format String Syntax|http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax]
          it keeps telling me that it cannot cast it to double
          Show your actual code, and paste in the exact, complete error message. Use the CODE button or [code] and [/code] tags to make your code readable. Don't provide a huge amount of code, just enough to show the problem, even if you have to create an entirely different program to do it--an SSCCE
          • 2. Re: chANGING FLOAT INTO DOUBLE
            3004
            Also, I don't know what "do[ing] a print out" has to do with JTextArea.

            Your question is really not clear.
            • 3. Re: chANGING FLOAT INTO DOUBLE
              843785
              it tried that first technique float f = 1.23; double d = f; it did not work and that is what i did before i even posted that is why i am confused.
              i am looking at my java reference book (yes the 20000000 million page thing with a tiger on the front) and it has BigDecimal(double val);, in the decimal section which seems to be the nearest thing that I can find to the decimal form. I need it to be decimal to get pound and pence or dollars and cents that is why the print out is important.

              the reason that I have the jtextarea for printing is because i put all of the information in the jtextarea to view in the gui then to do a print out that the reader can have also i have problems with printing out different components as soon as i put more than one component in it prints one on one sheet and then the next on another sheet and then so on and so on so this is only way that i can get all of that information on the page.

              i will look into my ref book more and see whether i can work it out if you can give me any more information on where to look specifically it would be helpful

              Edited by: Reg_Dwight on Aug 16, 2008 5:44 PM

              Edited by: Reg_Dwight on Aug 16, 2008 5:46 PM
              • 4. Re: chANGING FLOAT INTO DOUBLE
                843785
                hmm
                Object Float :
                d = ff.doubleValue();
                Both object's:
                dd = new Double(ff.floatValue());
                Both primitives :
                d = f;
                Object Double:
                dd = new Double(f);


                for much more ====> you might want to see http://mindprod.com/applet/converter.html
                • 5. Re: chANGING FLOAT INTO DOUBLE
                  843785
                  here is some of the code
                  double pr1 = num1;
                  double pr2 = num2;
                  double pr3 = num3;
                  double pr4 = num4;
                  double pr5 = num5;
                  double pr6 = result2;
                  double pr7 = result4;
                  double pr8 = result3;
                  double pr9 = result8;
                  double pr10 = q;
                  double pr11 = result9;
                  double pr12 = result10;
                  jTextArea1.setText(String.valueOf(
                           "                            " + pr1 + "\n" + "\n" +
                           "                            " + pr2 + "\n" + "\n" +
                           "                            " + pr3 + "\n" + "\n" +
                           "                            " + pr4 + "\n" + "\n" +
                           "                            " + pr5 + "\n" + "\n" +
                  "\n" + "\n" + "\n" + "\n" + "\n" + "\n" +        
                           "                                          " + pr6 + "\n" + "\n" +
                           "                                          " + pr7 + "\n" + "\n" + 
                           "                                          " + pr8 + "\n" + "\n" +
                           "                                          " + pr9 + "\n" + "\n" +
                           "                                          " + pr10 + "\n" + "\n" +
                           "                                          " + pr11 + "\n" + "\n" +
                           "                                          " + pr12 + "\n"      
                          ));
                  }
                  • 6. Re: chANGING FLOAT INTO DOUBLE
                    843785
                    i have tried all of the suggestion from nevermind but it still does not do it
                    • 7. Re: chANGING FLOAT INTO DOUBLE
                      3004
                      Reg_Dwight wrote:
                      it tried that first technique float f = 1.23; double d = f; it did not work
                      "Did not work" contains zero information.

                      If you want to display 2 decimal places, I gave you several links you can use.
                      • 8. Re: chANGING FLOAT INTO DOUBLE
                        3004
                        nevermind_pl wrote:
                        hmm
                        Object Float :
                        d = ff.doubleValue();
                        Both object's:
                        dd = new Double(ff.floatValue());
                        Both primitives :
                        d = f;
                        Object Double:
                        dd = new Double(f);
                        What does this have to do with the OP's question?
                        • 9. Re: chANGING FLOAT INTO DOUBLE
                          843785
                          it tried that first technique float f = 1.23; double d = f; it did not work
                          It might not have "worked" - but I bet it did what you asked: it created a double from a float.

                          Now we have Float, BigDecimal entering the picture, and who knows what else might be suggested by your huge book! The best thing to do would be to stop thinking about how and get clear about what. If I understand you correctly you have a floating point value (float or double or something like that) and you want to display it in a text area. AND you want the displayed string to look reasonable for the user.

                          (If this is not correct, please say.)

                          Now it's very easy to display text in a text area. So the problem boils down to how to obtain suitable text from your floating point value. This is a very general problem and the process of obtaining the textual form of something is formatting. (Note that in civilian life formatting means something a bit different: putting tabs and things in a string and making parts of it bold etc. That's not what's meant here.)

                          On the face of it the task of converting floating-point-value -> readable-string has nothing to do with printing, float/double conversion, Float, BigDecimal or any of the other things that have been raised. It's just formatting.

                          Did you read the Formatter (and other) links that jverd posted? This class is made to do the job that you're attempting. Unfortunately its usage is documented all over the place including one that he didn't link to (I think): [the Stirng format() method|http://java.sun.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)].

                          Check this out and see if it helps. It is a simple program which attempts to produce readable money like strings from floating point values. It just prints the string out so we can see them; in your case you would want to put the strings into your text area somehow.
                          public class PrintfEg {
                              public static void main(String[] args) {
                                  float f = 123.321f;
                                  double d = 42;
                                    
                                  String str = String.format("%.2f", f);
                                  System.out.println(str);
                          
                                  str = String.format("%.2f", d);
                                  System.out.println(str);
                          
                                  str = String.format("%.2f", 987.654);
                                  System.out.println(str);
                              }
                          }
                          To understand the cryptic "%.2f" string read the Formatter documentation already linked to.
                          • 10. Re: chANGING FLOAT INTO DOUBLE
                            843785
                            yes you are right i wan to display figures in pounds and pence in my jtextarea then print it out and at the moment it is showing more than two numbers after the decimal
                            • 11. Re: chANGING FLOAT INTO DOUBLE
                              843785
                              at the moment it is showing more than two numbers after the decimal
                              So replace the prN variables in reply 3 with "String.format("%.2f", numN)" and see what happens.
                              • 12. Re: chANGING FLOAT INTO DOUBLE
                                843785
                                yeah it worked thank you very much i greatly needed that help i have been looking for about the last five hours.

                                thanks again.
                                • 13. Re: chANGING FLOAT INTO DOUBLE
                                  843785
                                  You're welcome.

                                  The Formatter documentation is quite long, but the syntax can be quite useful and you only have to remember as much as you actually want to use. Things like getting the alignment right (lining up the decimal points when 1000.00 appears below 10.00) can be solved by carefully reading the details.
                                  • 14. Re: chANGING FLOAT INTO DOUBLE
                                    3004
                                    Reg_Dwight wrote:
                                    yeah it worked thank you very much i greatly needed that help i have been looking for about the last five hours.

                                    thanks again.
                                    If only somebody had suggested that in reply 1.