You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Olivier Sechet (JIRA)" <ji...@apache.org> on 2012/06/29 12:13:42 UTC

[jira] [Created] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Olivier Sechet created CLI-226:
----------------------------------

             Summary: createNumber() in TypeHandler cannot work with some Locale
                 Key: CLI-226
                 URL: https://issues.apache.org/jira/browse/CLI-226
             Project: Commons CLI
          Issue Type: Bug
    Affects Versions: 1.2
            Reporter: Olivier Sechet


The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).

With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.

For example:
{code:title=Type.java|borderStyle=solid}
public class Type {

    public static void main(final String[] args) {
        java.util.Locale.setDefault(java.util.Locale.GERMANY);
        String text = java.text.NumberFormat.getNumberInstance().format(12.34);
        Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
        System.out.println(nb);
    }
}
{code}


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Re: [jira] [Created] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Posted by Gary Gregory <GG...@rocketsoftware.com>.
Feel free to provide a patch! 

Gary

On Jun 29, 2012, at 6:12, "Olivier Sechet (JIRA)" <ji...@apache.org> wrote:

> Olivier Sechet created CLI-226:
> ----------------------------------
> 
>             Summary: createNumber() in TypeHandler cannot work with some Locale
>                 Key: CLI-226
>                 URL: https://issues.apache.org/jira/browse/CLI-226
>             Project: Commons CLI
>          Issue Type: Bug
>    Affects Versions: 1.2
>            Reporter: Olivier Sechet
> 
> 
> The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).
> 
> With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.
> 
> For example:
> {code:title=Type.java|borderStyle=solid}
> public class Type {
> 
>    public static void main(final String[] args) {
>        java.util.Locale.setDefault(java.util.Locale.GERMANY);
>        String text = java.text.NumberFormat.getNumberInstance().format(12.34);
>        Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
>        System.out.println(nb);
>    }
> }
> {code}
> 
> 
> --
> This message is automatically generated by JIRA.
> If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
> For more information on JIRA, see: http://www.atlassian.com/software/jira
> 
> 

[jira] [Commented] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-226?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13404714#comment-13404714 ] 

Sebb commented on CLI-226:
--------------------------

The Javadoc says:

{noformat}
/**
 * Create a number from a String. If a . is present, it creates a
 * Double, otherwise a Long.
 *
 * @param str the value
 * @return the number represented by <code>str</code>, if <code>str</code>
 * is not a number, null is returned.
 */
{noformat}

This specifically requires '.' as the decimal point separator, so fixing this will change the contract for Locales that don't use '.'. Not necessarily a blocker, but must be made clear in the release notes.

On the other hand, the Javadoc says null is returned if the input is not a number; that's not correct - the code never returns null, it throws ParseException.

As to the test failure, even without the fix AFAICT the test will fail if '.' is not the decimal separator. The fix is to ensure that the test is run in a Locale with the expected separators. 

If the change is made, we'd need to add further tests using other Locales, and ideally one that works regardless of Locale.
                
> createNumber() in TypeHandler cannot work with some Locale
> ----------------------------------------------------------
>
>                 Key: CLI-226
>                 URL: https://issues.apache.org/jira/browse/CLI-226
>             Project: Commons CLI
>          Issue Type: Bug
>    Affects Versions: 1.2
>            Reporter: Olivier Sechet
>              Labels: i18n
>
> The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).
> With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.
> For example:
> {code:title=Type.java|borderStyle=solid}
> public class Type {
>     public static void main(final String[] args) {
>         java.util.Locale.setDefault(java.util.Locale.GERMANY);
>         String text = java.text.NumberFormat.getNumberInstance().format(12.34);
>         Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
>         System.out.println(nb);
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Updated] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Posted by "Gary D. Gregory (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CLI-226?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gary D. Gregory updated CLI-226:
--------------------------------

    Attachment: cli-226.diff

This patch updates the number handling to accept "," and "." for decimal separators. This is a change in behavior from what is described in the Javadoc.
                
> createNumber() in TypeHandler cannot work with some Locale
> ----------------------------------------------------------
>
>                 Key: CLI-226
>                 URL: https://issues.apache.org/jira/browse/CLI-226
>             Project: Commons CLI
>          Issue Type: Bug
>    Affects Versions: 1.2
>            Reporter: Olivier Sechet
>              Labels: i18n
>         Attachments: cli-226.diff
>
>
> The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).
> With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.
> For example:
> {code:title=Type.java|borderStyle=solid}
> public class Type {
>     public static void main(final String[] args) {
>         java.util.Locale.setDefault(java.util.Locale.GERMANY);
>         String text = java.text.NumberFormat.getNumberInstance().format(12.34);
>         Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
>         System.out.println(nb);
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-226?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13404863#comment-13404863 ] 

Sebb commented on CLI-226:
--------------------------

The proposed Javadoc change is not strictly true, as ',' only works as described if it is the decimal separator.
The Javadoc should describe what actually happens.

Also, according to its Javadoc, the NumberFormat#parse() method does not necessarily use the full string, so this might allow trailing garbage.
                
> createNumber() in TypeHandler cannot work with some Locale
> ----------------------------------------------------------
>
>                 Key: CLI-226
>                 URL: https://issues.apache.org/jira/browse/CLI-226
>             Project: Commons CLI
>          Issue Type: Bug
>    Affects Versions: 1.2
>            Reporter: Olivier Sechet
>              Labels: i18n
>         Attachments: cli-226.diff
>
>
> The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).
> With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.
> For example:
> {code:title=Type.java|borderStyle=solid}
> public class Type {
>     public static void main(final String[] args) {
>         java.util.Locale.setDefault(java.util.Locale.GERMANY);
>         String text = java.text.NumberFormat.getNumberInstance().format(12.34);
>         Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
>         System.out.println(nb);
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (CLI-226) createNumber() in TypeHandler cannot work with some Locale

Posted by "Gary D. Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CLI-226?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13404503#comment-13404503 ] 

Gary D. Gregory commented on CLI-226:
-------------------------------------

The nice way to fix this is to rewrite {{org.apache.commons.cli.TypeHandler.createNumber(String)}} like this:

{code:java}
    public static Number createNumber(String str) throws ParseException
    {
        try
        {            
           return NumberFormat.getNumberInstance().parse(str);
        }
        catch (NumberFormatException e)
        {
            throw new ParseException(e.getMessage());
        } catch (java.text.ParseException e) 
        {
            throw new ParseException(e.getMessage());
        }
    }
{code}

But this makes one assert fail in {{org.apache.commons.cli.PatternOptionBuilderTest.testNumberPattern()}}

Thoughts?
                
> createNumber() in TypeHandler cannot work with some Locale
> ----------------------------------------------------------
>
>                 Key: CLI-226
>                 URL: https://issues.apache.org/jira/browse/CLI-226
>             Project: Commons CLI
>          Issue Type: Bug
>    Affects Versions: 1.2
>            Reporter: Olivier Sechet
>              Labels: i18n
>
> The {{createNumber()}} method in the {{TypeHandler}} class expects the decimal separator to be a dot ({{'.'}}). However the dot is not used in all the languages as a decimal separator. Most of the European countries, Russia and a lot of others countries uses a comma ({{','}}).
> With the corresponding {{Locale}}, the {{createNumber()}} method fails, throwing an exception.
> For example:
> {code:title=Type.java|borderStyle=solid}
> public class Type {
>     public static void main(final String[] args) {
>         java.util.Locale.setDefault(java.util.Locale.GERMANY);
>         String text = java.text.NumberFormat.getNumberInstance().format(12.34);
>         Number nb = org.apache.commons.cli.TypeHandler.createNumber(text);
>         System.out.println(nb);
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira