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)
======================================================================
- duplicates
-
JDK-4469922 Double and Float cannot converts string of Thai digits to a number
-
- Closed
-
- relates to
-
JDK-8326627 Document Double/Float.valueOf(String) behavior for numeric strings with non-ASCII digits
-
- Closed
-
-
JDK-4344834 [Fmt-Nu] Java SDK "1.3.0-C" localization issue with Double.parseDouble(String) method
-
- Closed
-