Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4449157

Float.parseFloat() method is not Internationalized

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0, 1.4.0
    • core-libs
    • beta
    • generic, x86
    • generic, windows_nt
    • Verified



      Name: boT120536 Date: 04/22/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      Hi
      There is some problem in the making code Internationalized can any one help me.
      I want to format the float value and want to set the One digit after decimal
      i.e. 1.222 as 1.2, for that I have used following line code.

      DecimalFormat df1= new DecimalFormat("###0.#");
      String str = df1.format(1.222);
      System.out.println(str);
      System.out.println(Float.parseFloat(str));

      It is working properly in US Locale.

      Now I change the Regional Setting from English (US) to the German through
      Control panel of the Windows OS, and generates the problem.

      The output of this code is first System.out.println () is. 1,2
      The last line is giving NumberFormateException
      The output 1,2(String) , because it is now German locale and in German 1.2 is
      1,2. And as you know the parseFloat method does not accept any char other then
      0-9 and some Alphabets (f, d, -, + ect.). This parseFloat method is seems to be
      Hardcode for decimal separation Symbol as “.”(DOT).

      The DecimalFormat class is perfectly internationalized, because on changing the
      regional setting it giving the proper format output. Now I try to again convert
      this Formatted String to Float value by Float.parseFloat() method then it is
      giving NumberFormateException. As this is not a float value in US Standard so
      it is giving NumberFormateException. That mean it is not supporting the
      Internationalization.
      I can solve this problem by hardcoding the and setting the locale of
      application to US, but this method is not at all acceptable, because client
      can’t enter and view the Inputs and outputs in their regional format.
      So is their any way even if we change the Regional Setting of OS it will not
      Effect the Output.
      If DecimalFormat class is following Internationalization rules then
      Float.parseFloat() methods should also support.
      The full program is as follows.

      import java.text.*;
      public class Try
      {
          public static void main(String[] aregs)
          {
              DecimalFormat df1= new DecimalFormat("###0.#");
              String str = df1.format(1.222);
              System.out.println(str);
              System.out.println(Float.parseFloat(str));
          }
      }

      Waiting for your Reply
      Kapil
      (Review ID: 120835)
      ======================================================================

            darcy Joe Darcy
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: