JDK-8185849 : NumberFormat.getPercentInstance().parse(String) does not work in Java 9
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2017-08-01
  • Updated: 2017-11-10
  • Resolved: 2017-08-07
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "9"
Java(TM) SE Runtime Environment (build 9+179)
Java HotSpot(TM) Server VM (build 9+179, mixed mode, emulated-client, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.14393]

A DESCRIPTION OF THE PROBLEM :
NumberFormat.getPercentInstance().parse(String) can not parse a percent value.

REGRESSION.  Last worked in version 8u152

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test program.


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.text.ParseException: Unparseable number: "75%"
        at java.base/java.text.NumberFormat.parse(Unknown Source)
        at TestJava9.main(TestJava9.java:7)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.NumberFormat;
import java.text.ParseException;

public class TestJava9 {

    public static void main( String[] args ) throws ParseException {
        double value = NumberFormat.getPercentInstance().parse( "75%" ).doubleValue();
        System.out.println( value );
    }
}
---------- END SOURCE ----------


Comments
UTS#35 Unicode Locale Data Markup Language (LDML) is the specification of the XML format used for CLDR data, including the interpretation of the CLDR data. Here is the link http://www.unicode.org/reports/tr35/
07-08-2017

As per JEP 252, CLDR has become default locale provider in jdk9. In this very example, NumberFormat.getPercentInstance(new Locale("de", "DE")).parse("75%").doubleValue();, CLDR provides "#,##0\u00a0%" as a pattern for parsing/formatting a percent value, hence parsing expects user to input "75\u00a0%" (a non-breaking space between number and percent sign). To stick to the behavior compatible to JDK8, use -Djava.locale.providers=COMPAT,SPI system property while using java runtime.
07-08-2017

NumberFormat.getPercentInstance(new Locale("de", "DE")).parse("75%").doubleValue(); throws same exception as submitter is getting Exception in thread "main" java.text.ParseException: Unparseable number: "75%" One workaround is to use -Djava.locale.providers=JRE. Other way is he can correct his code by changing "75%" with "75\u00a0%" or by adding non-blocking space before percentage. But it wont work with normal space "75 %". Is it expected?
04-08-2017