6 Replies Latest reply: Apr 8, 2008 9:32 AM by mlk RSS

    Casting problem

    807601
      Hi every body,

      I am getting following exception while running the following code. I checked it and casted it carefully, but I do not know why it is displaying the error

      double diffperc1=diff/previousPrice*100;
      diffperc1=Math.abs(diffperc1);
      if (diffperc1 >= 25){
           NumberFormat formatter=new DecimalFormat("0.##");
           String stringdiffperc=formatter.format(diffperc1);
           double diffperc=Double.parseDouble(stringdiffperc);
      }
      And the error is as
      java.lang.NumberFormatException: For input string: "25,89"
           at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
           at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1207)
           at java.lang.Float.parseFloat(Float.java:222)
           at CheckKursSplit.KursSplitInfo.getPricesDate(KursSplitInfo.java:86)
           at CheckKursSplit.KursSplitInfo.main(KursSplitInfo.java:123)
      Exception in thread "main"

      Thanks in advance
      kind regards,
        • 1. Re: Casting problem
          mlk
          >
          Please use code tags next time. Highlight the code and press the code button above.

               NumberFormat formatter=new DecimalFormat("0.##");
          Are you in a locale that uses "," as a "decimal separator or monetary decimal separator" (like say most of Europe)?
          If so if you want to force it to use "." not the locale format then either use the factory methods in NumberFormat, or use "0'.##"
          • 2. Re: Casting problem
            807601
            Hi mlk
            Thank you very much.
            Can you tell me that how can I use the factory method to resolve the error.

            Best regards
            • 3. Re: Casting problem
              800308
              Dude, read the API doco... you're looking for the get*Instance methods.
              • 4. Re: Casting problem
                807601
                Thanks for reply.

                Can you help me.
                I have used the getInstance() as you refered me. But it is displaying the same error.

                NumberFormat nf= NumberFormat.getInstance();
                String stringdiffperc=nf.format(diffperc1);
                double diffperc=Double.parseDouble(stringdiffperc)

                Best regards,
                • 5. Re: Casting problem
                  807601
                  I try again using the getInstance method as follow but found the same error. I want to show only two digits in diffperc1 result. Suppost diffperc1 =20.47 only. Can any body help me. Thanks



                  double diff=price-previousPrice; //price=17.15 and previousPrice=21.69 and diff=4.44

                  double diffperc1=diff/previousPrice*100; //diffperc1=20.47026279391
                  diffperc1=Math.abs(diffperc1);
                  if (diffperc1 >= 25){
                       NumberFormat nf= NumberFormat.getInstance(Locale.GERMAN);
                       String stringdiffperc=nf.format(diffperc1);
                       double diffperc=Double.parseDouble(stringdiffperc);
                  }
                  • 6. Re: Casting problem
                    mlk
                    >
                    Please use code tags. After copy & pasting you code into the edit window, highlight it and press the little button marked "code"

                         NumberFormat nf= NumberFormat.getInstance(Locale.GERMAN);
                    OK, my last post was not clear enough.

                    Are you in a locale that uses "," as a "decimal separator or monetary decimal separator" (like say most of Europe)?
                    If so if you want to force it to use "." not the locale format then either use the factory methods in NumberFormat by specifying a locale that does not use "," as the "decimal separator or monetary decimal separator", or use "0'.##"